Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/431.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在目录上设置cookie_Javascript_Cookies - Fatal编程技术网

Javascript 在目录上设置cookie

Javascript 在目录上设置cookie,javascript,cookies,Javascript,Cookies,我将cookie设置为记住类名更改,但是如果我将cookie设置为@mydomain/它将读取所有目录路径,但是如果cookie设置为mydomain/sub/它将仅读取该目录路径 有什么建议吗 HTML: <html> <head> <script type="text/javascript" src="theme.js" /></script> </head> <body id="site-index" c

我将cookie设置为记住类名更改,但是如果我将cookie设置为@mydomain/它将读取所有目录路径,但是如果cookie设置为mydomain/sub/它将仅读取该目录路径

有什么建议吗

HTML:

<html>
  <head>
    <script type="text/javascript" src="theme.js" /></script>
  </head>
  <body id="site-index" class="defaultTheme">
    <a href="#" id="themeone">Theme one</a>
    <a href="#" id="themetwo">Theme Two</a>
  </body>
</html>

在设置cookie时,您可以特别将路径设置为“/”,而不管设置它时目录结构有多深

因此,在setCookie函数中,只需调整以下内容:

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


…或者,如果需要灵活性,可以向可以传入的函数添加路径参数。

您有两个setCookie函数

删除第一个

//饼干

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

setCookie(“StateTheme”,StateTheme,expiryDate,”/”


我不知道是谁在我的代码中添加了另一个setCookie函数,但你发布的cookie代码肯定是我拼凑的:)

毫无疑问,我在这里的某个地方找到了它!我肯定会在代码中添加注释,以表彰您的工作。谢谢你的帮助!请不要担心要感谢的人是比尔·多奇:)看到这段代码很有趣。如果我的回答有帮助,请
function setCookie() {
exp=new Date();
plusMonth=exp.getTime()+(31*24*60*60*1000);
exp.setTime(plusMonth);
document.cookie='StateTheme='+stateTheme+';expires='+exp.toGMTString();
}