javascript 为什么在 Chrome 中设置 document.cookie 不起作用?

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

Why would setting document.cookie not work in Chrome?

javascriptgoogle-chrome

提问by Dave Stein

My coworker ran into an issue where NO cookie could be set on Chrome via code like this:

我的同事遇到了一个问题,即无法通过以下代码在 Chrome 上设置 cookie:

document.cookie = "TEST=1; expires=Tue, 14 Oct 2014 20:23:32 GMT; path=/"

document.cookie = "TEST=1; expires=Tue, 14 Oct 2014 20:23:32 GMT; path=/"

Putting document.cookieinto the console immediately after would show results as if I made no change. On refresh of the page, the cookie was not there so it was reporting correctly, just not setting correctly.

document.cookie到控制台后,立即会显示结果,如果我没有进行任何更改。刷新页面时,cookie 不在那里,所以它报告正确,只是设置不正确。

The above code would work if he opened a new incognito window and worked for everyone else in general. I removed all his cookies using the dev tools and still had no luck manually setting cookies ( although others would come back that were set via the server headers).

如果他打开一个新的隐身窗口并为其他所有人工作,则上述代码将起作用。我使用开发工具删除了他的所有 cookie,但仍然没有运气手动设置 cookie(尽管其他人会回来通过服务器标头设置)。

Once he restarted Chrome, it started to behave properly, so it seems like he was running up against some quirk or bug that can no longer be reproduced.

一旦他重新启动 Chrome,它就开始正常运行,所以他似乎遇到了一些无法再重现的怪癖或错误。

Has anyone else run into this? As of now I am thinking of checking that document.cookiereports back what is expected after setting, and then initiating our cookieless flow for when a user has cookies disabled when things don't match up. I hate the idea of doing that so any suggestions / answers would be great.

有没有其他人遇到过这个问题?到目前为止,我正在考虑检查是否document.cookie报告设置后的预期内容,然后在用户禁用 cookie 时启动我们的 cookieless 流程,当事情不匹配时。我讨厌这样做的想法,所以任何建议/答案都会很棒。

回答by avetisk

The way cookies work, at least in Chrome, is a bit weird.

cookie 的工作方式,至少在 Chrome 中,有点奇怪。

If you need to change a cookie's value, then you need to add/set each keys one by one.

如果您需要更改 cookie 的值,那么您需要一个一个地添加/设置每个键。

Try this in your console:

在您的控制台中试试这个:

document.cookie; // -> "expires=Tue, 14 Oct 2014 20:23:32 GMT; path=/"
document.cookie = 'TEST=1';
document.cookie; // -> "TEST=1; expires=Tue, 14 Oct 2014 20:23:32 GMT; path=/"

Yes, it has added the key, and not replace the whole cookie with TEST=1.

是的,它添加了密钥,而不是将整个 cookie 替换为TEST=1.

If you need to remove a key, you can simple provide no value: TEST=.

如果您需要删除一个键,您可以简单地不提供任何值:TEST=

I hope this will get you out of the cookie nightmare (it was for me).

我希望这能让你摆脱饼干的噩梦(这是给我的)。

回答by Prateek

Make sure to run it on a server (at least a local server) so that document.cookie works.

确保在服务器(至少是本地服务器)上运行它,以便 document.cookie 工作。

If you locally run this file in the browser. "document.cookie" wouldn't work.

如果您在浏览器中本地运行此文件。“document.cookie”不起作用。

回答by JstnPwll

As another user mentioned, you have to set them one-by-one. These functions can be useful in parsing & applying a cookie string:

正如另一位用户所提到的,您必须逐一设置它们。这些函数可用于解析和应用 cookie 字符串:

function clearCookies(){
    var cookies = document.cookie.split(';');
    for(i in cookies){
        var vals = cookies[i].split('=');
        var name = vals.shift(0, 1).trim();
        document.cookie = name+'=';
    }
}
function parseCookies(cookie){
    clearCookies();
    var cookies = cookie.split(';');
    for(i in cookies){
        var vals = cookies[i].split('=');
        var name = vals.shift(0, 1).trim();
        document.cookie = name+'='+vals.join('=');
    }
}