Javascript document.cookie 总是空字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15914744/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 02:34:54  来源:igfitidea点击:

Javascript document.cookie always empty string

javascriptcookies

提问by Menztrual

I have this real strange problem with client side javascript setting cookies. I'm developing a little 1 page demo at the moment to use cookies to store some 'preferences'. Please note that I can't use a server side language for this demo or any 3rd party jQuery plugins.

我在客户端 javascript 设置 cookie 时遇到了这个真正奇怪的问题。我目前正在开发一个小小的 1 页演示,以使用 cookie 来存储一些“首选项”。请注意,我不能在此演示或任何 3rd 方 jQuery 插件中使用服务器端语言。

So I've written a javascript object to set a cookie:

所以我写了一个 javascript 对象来设置 cookie:

var cookie = {
  set: function (name,value,exdays) {

    var exdate = new Date();
    exdate.setDate(exdate.getDate() + exdays);
    var value = escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
    document.cookie=name + "=" + value;
    console.log(document.cookie);
  }
}

cookie.set('foo','bar',2);
console.log(document.cookie);

It just returns an empty string. I've gone into Chrome console to see if I can do it via directly modifying document.cookie

它只返回一个空字符串。我已经进入Chrome控制台,看看是否可以通过直接修改来完成document.cookie

> document.cookie = "foo=bar";
"foo=bar"
> document.cookie
""

How do you set a cookie via client side javascript?

你如何通过客户端javascript设置cookie?

Edit: I am not in incognito mode and cookies are enabled.

编辑:我没有处于隐身模式并且启用了 cookie。

回答by Stijn de Witt

HttpOnly cookies cannot be accessed from Javascript and session cookies are usually set as HttpOnly cookies. See also this StackOverflow question: How to read a secure cookie using JavaScript

无法从 Javascript 访问 HttpOnly cookie,会话 cookie 通常设置为 HttpOnly cookie。另请参阅此 StackOverflow 问题: How to read a secure cookie using JavaScript

So... check whether the cookie you want to read has the 'HttpOnly' flag set... If so, you know the culprit. It's not a bug, it's a feature!

所以...检查您要读取的 cookie 是否设置了 'HttpOnly' 标志...如果是,您就知道罪魁祸首了。这不是一个错误,这是一个功能!

回答by Menztrual

You can't set cookies by the look of things if its not running in a web server.

如果 cookie 不在 Web 服务器中运行,则无法通过外观设置 cookie。

file:///C:/Users/me/Desktop/demo/demo.html

file:///C:/Users/me/Desktop/demo/demo.html

however:

然而:

http://localhost/demo/demo.htmlworks.

http://localhost/demo/demo.html作品。

回答by Aram Kocharyan

For usage and docs, see here:

有关用法和文档,请参见此处:

https://developer.mozilla.org/en-US/docs/DOM/document.cookie

https://developer.mozilla.org/en-US/docs/DOM/document.cookie

If you are in Incognito Modeor have cookies disabled, it won't work.

如果您处于隐身模式或禁用 cookie,它将无法工作。

回答by poida

This worked for me when ran from localhost, running chrome 28.0.1472.0 canary:

当从本地主机运行时,这对我有用,运行 chrome 28.0.1472.0 canary:

<!DOCTYPE html>
<html>
<head>
  <title>localhost cookie</title>
</head>
<body>
  <script type="text/javascript">
    console.log(document.cookie);
    var myCookie = "mycookie=hellocookie";
    document.cookie = myCookie;
  </script>
</body>
</html>

Run it in a server, visit the page and look at your cookie store, refresh the page and look at your console.

在服务器中运行它,访问页面并查看您的 cookie 存储,刷新页面并查看您的控制台。

It did not set a cookie when opened as a file but worked every time when opened from the server.

它在作为文件打开时没有设置 cookie,但每次从服务器打开时都可以工作。