views:

13

answers:

1

This is my JavaScript code I use to create my cookie............

document.cookie = "Name=" + Name + ";expires=Friday, 31-Dec-2011 12:00:00 GMT; path/";

I create it in www.example.com/folder/file.html and it works.

But I cant read the cookie from www.example.com/index.html or www.example.com/folder2/file2.html.

What is wrong with my code?

Thanks in advanced

+1  A: 

you have an error in how you set the path

 path/ 

should be

path=/

Also, the format for the expiration date is wrong - it should be like Thu, 2 Aug 2001 20:47:11 UTC, the same format that is returned by new Date().toGMTString();.

Sean Kinsey
It might very well be the expiration date as my modified answer points out.
Sean Kinsey
Actually the bizarro time format with the `-` and `GMT` is correct according to the original Netscape cookie specification (http://curl.haxx.se/rfc/cookie_spec.html). The output of `toUTCString` (which is the same as the deprecated `toGMTString`) didn't used to be accepted for cookie expiry dates by all browsers, though I believe modern browsers do also allow it.
bobince