CastlesBlog.com

a blog about a blog

Subscribe

Same name cookie with a different path

I've been having mixed results setting and retrieving cookies with the same name and different paths. A cookie set with a path of "/" is accessible anywhere within a site. I wanted my cookies to be different depending on the URL. My solution is to name cookies in the root of a website different to the rest. Pretty simple really, just took me far too long to work this out.

var cookie_name = 'my_great_cookie';
if(document.location.pathname == '/') {
    cookie_name = 'root_' + cookie_name;
}
//set/retrive cookie here
Post Comment