我有一个函数可以设置一个 cookie 来记住点击的图像。如果该文件位于根文件夹中,则一切正常。如果我将相同的文件放在子文件夹中,则不会设置 cookie。
这是我的代码。
function FensterOeffnen (Variable) {
var a = new Date();
a = new Date(a.getTime() +1000*5);
document.cookie = 'uebergabe='+Variable+'; expires='+
a.toGMTString()+';';
window.location.href="../test2.html";
}
最佳答案
这就是 document.cookie
默认情况下的工作方式。由于您没有为 cookie 指定路径,因此默认为您的脚本所在的子文件夹。引用:
Any of the following cookie attribute values can optionally follow the key-value pair, specifying the cookie to set/update, and preceded by a semi-colon separator:
;path=path
(e.g., '/', '/mydir') If not specified, defaults to the current path of the current document location.
因此您设置的 cookie 的路径为例如/我的目录
。这意味着它对该路径之外的脚本无效 according to RFC 6265 .
如果您希望整个站点都可以访问 cookie,则必须手动添加 ;path=/
:
document.cookie = 'uebergabe='+Variable+'; expires='+
a.toGMTString()+';path=/';
关于javascript - 子文件夹中未设置 cookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20491860/