PDA

View Full Version : Problem Writing Cookies in Safari


crees!
Mar 3, 2004, 06:13 PM
So I'm having this is problem where Safari will only write cookies that I make if they just have a name and value. If I set the expiration or path it will not write the cookie. I know my code is correct because I'm taking it straight from a web book. Plus I checked Safari's Developer FAQ's and they had the same example. So I don't know what gives.


This works:

var cartName = "AppleCart";

var expiration = new Date();
expiration.setMinutes(expiration.getMinutes() + 120);

function writeCookie(value)
{
document.cookie = cartName + "=" + escape(value);

if (document.cookie != "")
alert("Cookie has been written");
else
alert("Cookie has not been saved");
}


This does not work:

function writeCookie(value)
{
document.cookie = cartName + "=" + escape(value) + "; expires=" + expiration.toGMTString();

if (document.cookie != "")
alert("Cookie has been written");
else
alert("Cookie has not been saved");
}

crees!
Mar 3, 2004, 06:49 PM
Well I realized Safari doesn't like using setMinutes(), but instead setDate() but correctly write the cookie. Still, this is interesting. But then maybe I'm just a nub. ;)

crees!
Mar 3, 2004, 06:55 PM
And now finally reading up further I see that Minutes are from 0-59.. so setting it + 120 sort of breaks that doesn't it...

Westside guy
Mar 3, 2004, 07:15 PM
I've ran into that before - Safari's date and time handling seem to be a lot pickier than with Mozilla or IE. I was passing the date to my cookie routines and then using a Date() object to convert it; other than with Safari I didn't have to be too careful about the format of my input string.