Shawn
Shawn

Reputation: 3159

jquery cookie not working - same domain different folder

I have a html page in a sub directory:

http://mydomain.com/subDir/mag.html

That file sets a cookie using $.cookie, then loads the main index page like so:

$.cookie("something",value);
alert( $.cookie("something") );  // test code: this shows the cookie correctly
window.open("../index.html","_self");

But when I do that, I loose the cookie (shows as undefined). If I put mag.html on the same directory level as index.html, it works.

I can work around this but was wondering why this is so, since the domain hasn't changed.

Upvotes: 1

Views: 1548

Answers (1)

user2699477
user2699477

Reputation:

One path doesn't have access to another's cookies, unless it is a lower level path (that is, closer to the 'core').

Your cookie is set in subDir. You have to set it to the path of the main index file so to be able to access this cookie there.

In order to set a custom path, you will have to pass a third argument to the $.cookie() method, which is a configuration object.

So: $.cookie("something", value, { path: '/' });

Upvotes: 3

Related Questions