Thursday, August 2, 2007

Redirect homepage too fast makes cookie setting failed in IE7

Yesterday I got a problem of setting cookies with Javascript in IE7. The cookie string was always generated successfully, but the cookie could not be stored by IE7. I set the privacy level of IE7 to "Accept all cookies", but it doesn't work yet. A simplified demo of the Javasript code is listed below:







<script language="JavaScript">
    var name="visitor";
    var value=Math.random();
    var today = new Date();
    var expires=new Date(today.getTime()+(86400*1000));
    var path="";
    var domain="";
    var secure="";
    var cookieString = name + "=" +escape(value
       ( (expires";expires=" + expires.toGMTString() ""
       ( (path";path=" + path : ""
       ( (domain";domain=" + domain : ""
       ( (secure";secure" "");  
    document.cookie = cookieString;
</script>
<html><head><META HTTP-EQUIV="Refresh" CONTENT="0; URL=http://www.google.com"></head></html>




Finally I found the reason. In order to store the cookies in IE7, you must manually "allow the active contents" to run.

However, the code <META HTTP-EQUIV="Refresh" CONTENT="0; URL=http://www.google.com"> refreshes the webpage too fast and thus you cannot unblock the active contents in time.

For <META HTTP-EQUIV="Refresh" CONTENT="n;url" > , n means the number of seconds to wait before loading the specified URL. If we increase this n (e.g., let n=10 ), then we will have enough time to unblock the active contents, and then the cookie can be stored successfully.

1 comment:

Dora said...

Keep up the good work.