document.cookie不存储cookie?
document.cookie 只能在服务器上工作吗?警报似乎没有输出我的 cookie。
document.cookie = "d=e";
alert(document.cookie);
Does document.cookie only work on a server? Alert doesn't seem to be outputting my cookie.
document.cookie = "d=e";
alert(document.cookie);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当由使用
file
协议的文档设置时,某些浏览器不会接受 cookie。来源。
Some browsers won't honor cookies when set by a document with the
file
protocol.Source.
Cookie 与特定域相关联。如果您只是将 HTML 文档作为文件打开,那么浏览器如何知道将 cookie 与哪个域关联,或者应该允许该文件访问哪些 cookie?
因此,是的,出于所有实际目的,如果您的 HTML 文档由服务器提供服务/通过 HTTP URL 访问(允许它确定某种域来关联 cookie),则您的浏览器将仅允许存储和检索 cookie和。
Cookies are associated with a particular domain. If you just open your HTML document as a file, then how is the browser to know what domain to associate a cookie with, or what cookies it should allow that file to access?
So yes, for all practical purposes, your browser will only allow cookies to be stored and retrieved if your HTML document is served by a server/accessed via an HTTP URL that allows it to determine some sort of domain to associate the cookie(s) with.