Skip to content Skip to sidebar Skip to footer

Setting Cookies On Directories

I'm setting a cookie to remember a className change, however if I set the cookie @ mydomain/ it reads for all directory paths, but if the cookie is set at mydomain/sub/ it only rea

Solution 1:

You have two setCookie functions.

Delete this first one

//COOKIES

functionsetCookie() {
exp=new Date();
plusMonth=exp.getTime()+(31*24*60*60*1000);
exp.setTime(plusMonth);
document.cookie='StateTheme='+stateTheme+';expires='+exp.toGMTString();
}

and use

setCookie("StateTheme",stateTheme,expiryDate,"/");

I do not who added that other setCookie function to my code - but the cookie code you posted is definitely one I put together :)

Solution 2:

You can specifically set the path to "/" when you set the cookie, regardless of how deep in the directory structure you are when you set it.

https://developer.mozilla.org/En/Document.cookie

So in your setCookie function, just tweak this:

document.cookie='StateTheme='+stateTheme+';expires='+exp.toGMTString()+',path=/';

...or add a path argument to the function that you can pass in, if you need the flexibility.

Post a Comment for "Setting Cookies On Directories"