Jump to content

I must have done a stupid mistake


niveanvp73

Recommended Posts

Hi. I just cannot make this simple cookie work... The variable that should contain the date of visitor's last visit is "last". The stupid is it works when I write alert(last) or document.write(last) but I wanted to tie to the span with ID="temp" as you see in the bottom....but it doesn't work. You can try it at: http://sweb.cz/nj.teplice/ ...the welcome message for "first-time-visitors" works too...I think there must be some stupid mistake or I don't know...<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>Cookie</title> <script type="text/javascript"> readCookie(); function readCookie() { if (document.cookie == "") { writeCookie(); alertMessage(); } else { var the_cookie = document.cookie; the_cookie = unescape(the_cookie); the_cookie_split = the_cookie.split(";"); for (loop=0;loop<the_cookie_split.length;loop++) { var part_of_split = the_cookie_split[loop]; var find_name = part_of_split.indexOf("nfti_date") if (find_name!=-1) { break; } // Close if } // Close for if (find_name==-1) { writeCookie(); } else { var date_split = part_of_split.split("="); var last = date_split[1]; last=fixTheDate(last); document.getElementById("temp").innerHTML=last; writeCookie(); } // Close if (find_name==-1) } } // Close function readCookie() function writeCookie() { var today = new Date(); var the_date = new Date("December 31, 2023"); var the_cookie_date = the_date.toGMTString(); var the_cookie = "nfti_date="+escape(today); var the_cookie = the_cookie + ";expires=" + the_cookie_date; document.cookie=the_cookie } function alertMessage(){ alert ("Welcome this is your first visit") } function fixTheDate(date) { var split = date.split(" "); var fix_the_time = split[3].split(":") var hours = fix_the_time[0] if (hours>=12) { var ampm="PM" } else { var ampm="AM" } if (hours > 12) { hours = hours-12 } var new_time = hours+":"+fix_the_time[1]+" "+ampm var new_date = split[0]+" "+split[1]+", "+split[2]+" at "+new_time+", "+split[5] return new_date; } </script> </head> <body> Your last visit: <span id="temp"> </span></body> </html>

Link to comment
Share on other sites

Try putting the function definitions before you call them. Also, you need to put the element assignment (the bit in bold) inside the window.onload event as the span isn't there when you try to write to it at the moment.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...