views:

652

answers:

2

Safari 4 Beta on Windows (build 528.17) has a bug. If expires is in value assigned to document.cookie, cookie is not changed. So, below cookie enable detection doesn't work anymore.

var dt = new Date();
dt.setSeconds(dt.getSeconds() + 2);

document.cookie = "cookietest=1; expires=" + dt.toGMTString();
var cookiesEnabled = document.cookie.indexOf("cookietest=") != -1;

If I just remove the expires, it works. However, it will leave a dummy cookie "cookietest=1" in the whole session.

I tried to operate in document.cookie directly to remove the cookie, but it seems there is no way to do that. The only way to "remove" a cookie is to set it expire.

Is there any workaround for this?

UPDATE: There is a bug in Safari 4 Beta for windows. if the expires is no more than 1 hour (3600 seconds) since now, assigning document.cookie would fails.

We can remove the cookie after detection.

var dt = new Date();
dt.setSeconds(dt.getSeconds() + 2);

document.cookie = "cookietest=1";
var cookiesEnabled = document.cookie.indexOf("cookietest=") != -1;

document.cookie = "cookietest=1; expires=Thu, 01-Jan-1970 00:00:01 GMT";
+1  A: 

Hi Morgan,

i. The error has nothing to do with safari build. The problem is with the date that you are setting. When you use dt.SetSeconds(dt.getSeconds()+2), you are setting the cookie expires time to currenttime+2 seconds. So by the time you load the page and check the cookie the cookie expires.

ii. Increase the time to more than a minute (60) and check, it will work.

Ramjee

rAm
Do you mean the functions with signature setSeconds/getSeconds is actually about milliseconds instead of seconds?The same code works fine in Firefox and IE and Safari 3.X.
Morgan Cheng
Hi Morgan, Yes, you are right. Sorry for not checking out. However, I still stand by what I said about the cookie being expired. Try by providing higher value and check if it works.
rAm
I would recommend you increase the figure drastically up from 2 just to see if it makes a difference. If you use 10000 instead, what happens? If it still doesn't work - the problem is something else.
Sohnee
A: 

It's true - there's a problem with "shortlived" cookies on Safari (on Windows at least) If the cookie is set to expire in less than 60 minutes - it expires immediately.

T4NK3R