Javascript 为什么 document.cookie 不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6774848/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why document.cookie is not working
提问by pavan
var curCookie = name + "=" + value +
"; expires=" + ATS_getExpire() +
"; path=" + path +
"; domain=" + domain ;
document.cookie = curCookie;
alert("Your Cookie : " + document.cookie);
When i use above code the alert message coming as empty. Why document.cookie is coming as empty. Please anybody answer.
当我使用上面的代码时,警报消息为空。为什么 document.cookie 是空的。请任何人回答。
回答by Raynos
See here for a Live Example
请参阅此处获取实时示例
You're using ;
instead of ,
.
您正在使用;
而不是,
.
Use ,
to deliminate your cookie values
使用,
到deliminate您的cookie值
var curCookie = name + "=" + value +
", expires=" + ATS_getExpire() +
", path=" + path +
", domain=" + domain;
document.cookie = curCookie;
alert("Your Cookie : " + document.cookie);
回答by krd
Sometimes this can occur if the page is hosted on a domain listed on the public suffix list(e.g. github.io, cloudfront.net). These domains are treated specially by the browser and restrict writing cookies for security reasons.
如果页面托管在公共后缀列表中列出的域(例如 github.io、cloudfront.net)上,有时会发生这种情况。这些域由浏览器特别处理并出于安全原因限制写入 cookie。
回答by RunningAdithya
Try using jQuery Cookie plugin:
尝试使用 jQuery Cookie 插件:
$.cookie('the_cookie', 'the_value', { expires: 7, path: '/' });