Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

crees!

macrumors 68020
Original poster
Jun 14, 2003
2,015
241
MD/VA/DC
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!

macrumors 68020
Original poster
Jun 14, 2003
2,015
241
MD/VA/DC
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!

macrumors 68020
Original poster
Jun 14, 2003
2,015
241
MD/VA/DC
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

macrumors 603
Oct 15, 2003
6,340
4,158
The soggy side of the Pacific NW
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.