Cookies issue with IE - need help!

JonTheBaller

Golden Member
Dec 2, 2002
1,916
0
0
I'm using JavaScript to set a cookie. When I set the path attribute of the cookie to "/", everything works fine. However, I want to limit access to the cookie to the page that created it. Thus, I want to be able to set the path attribute to location.pathname (for example /home/src/accounts.html). I can set cookies fine in IE and Mozilla, but only Mozilla is able to get the cookie. IE can't match the cookie path for some reason. Any ideas? Your help is greatly appreciated!
 

MrChad

Lifer
Aug 22, 2001
13,507
3
81
A cookie path should represent a directory, not an individual file. From MSDN:

In a similar manner to domains, the path attribute of a cookie restricts a cookie's visibility to a particular part of a web-server's directory tree. A web page such as http://www.microsoft.com/jscript might have a cookie with path '/jscript', which is only relevant to the JScript pages of that site. If a second cookie with the same name and domain also exists, but with the path '' (equivalent to '/'), then the web page would only see the first cookie, because its path is a closer match to the URL's path.

Paths represent directories, not individual files, so '/usr/local/tmp' is correct, but '/usr/local/tmp/myfile.htm' isn't. Forward slashes ('/' not '\') should be used. Trailing slashes as in '/usr/local/tmp/' should be avoided. That is why the top-level path is '' (a zero-length string), not '/'.