...

View Full Version : Cookie program not working



NanakiXIII
01-01-2004, 08:53 PM
I wrote this cookie script to see if I knew how, but it's not working. That yellow exclamation mark appears in the IE status bar, but there are no error messages, so I tried running it through a debugger, which returned the error that a '}' was expected at line 62 of the script, that's the last line, which is present. I think the error is in the checkcookie() function, which is called by an onLoad in the body tag, as I made an alert in the setcookie() function, which is supposed to load, as its the first time I run it, to see if I got all the variables right, which doesn't pop up.



function fixtime(number) //function to fix minutes and seconds
{
if (number < 10)
{
number = "0" + number;
}
return number;
}

function setcookie() //function that sets cookie
{
//vars for cookie info
var name = prompt("What's your name?", "");
var date = new Date();
var year = date.getFullYear();
var num_month = date.getMonth();
var day_of_month = date.getDate()
var num_day = date.getDays();
var hour = date.geHours();
var min = date.getMinutes();
var fixed_min = fixtime(min);
var sec = date.getseconds();
var fixed_sec = fixtime(sec);
var days = new Array("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday");
var months = new Array("January", "Februari", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
var month = months[num_month]
var day_of_week = days[num_day]

alert (hour + ":" + fixed_min + ":" + fixed_sec + ", " + day_of_week + " " + month + " " + day_of_month + ", " + year)

//vars for cookie expiration date
var expire_date = new Date("Januari 1, 2050");
var gmt_expire_date = expire_date.toGMTstring();

//build and save cookie
var cookie = "rpglyrics=" + escape(name + "/" + hour + ":" + fixed_min + ":" + fixed_sec + "/" + day_of_week + " " + month + " " + day_of_month + ", " + year);
cookie = cookie + ";expires=" + gmt_expire_date;
document.cookie = cookie;
}

function readcookie() //function that reads cookie and greets returning user
{
// take cookie, unescape and split name/time/date
var cookie = document.cookie;
var cookie = unescape(cookie);
var split_cookie = cookie.split("/")

// message for returning user
alert ("Hello, " + split_cookie[0] + ", the last time you visited was on " + split_cookie[3] + ", at " + split_cookie[2] + ".")
}

function checkcookie()
{
if (document.cookie.length == 0)
{
setcookie();
}
else
{
readcookie();
{
}

COBOLdinosaur
01-01-2004, 11:55 PM
at the end after readcookie() you have { instead of }

NanakiXIII
01-02-2004, 12:34 AM
Thanks. Strange it directed me to line 62 instead of 61 though.

Now there aren't any errors anymore, but the script still isn't working like I want it to. It runs but doesn't save anything. Now my question, does the page need to be on a server for cookies to work? I kind of forgot about that when I wrote it.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum