为什么设置document.cookie在Chrome中不起作用? [英] Why would setting document.cookie not work in Chrome?

查看:2885
本文介绍了为什么设置document.cookie在Chrome中不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的同事遇到了一个问题,可以通过如下代码在Chrome上设置NO Cookie:
$ b document.cookie =TEST = 1; expires = 2014年10月14日星期二20:23:32 GMT; path = /



放置 .cookie 会立即显示结果,就好像我没有改变。刷新页面时,Cookie不存在,所以报告正确,只是没有正确设置。



如果他打开一个新的隐身窗口,一般为其他人工作。我使用开发工具删除了所有的cookies,并且仍然没有手动设置cookie(尽管其他人会通过服务器头设置回来)。



一旦他重新启动Chrome,它开始表现得很好,所以看起来他正在运行一些怪癖或bug,无法再被复制。



有没有其他人遇到过这种情况?到目前为止,我正在考虑检查 document.cookie 在设置后报告什么是预期的,然后启动我们的无cookie的流程,以便当用户禁用Cookie时, t匹配。我讨厌这样做,所以任何建议/答案都会很棒。

解决方案

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



如果您需要更改cookie的值,则需要逐个添加/设置每个键



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

  document.cookie; //  - > expires = 2014年10月14日星期二20:23:32 GMT; path = /
document.cookie ='TEST = 1';
document.cookie; // - > TEST = 1; expires = 2014年10月14日星期二20:23:32 GMT; path = /

是的,它已经添加了密钥,而不是用 TEST = 1 替换整个cookie。



如果你需要删除一个键,你可以简单地提供没有价值: TEST =



我希望这会得到你超出了cookie的噩梦(这是给我的)。


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

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

Putting document.cookie into 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.

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).

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.

Has anyone else run into this? As of now I am thinking of checking that document.cookie reports 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.

解决方案

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

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

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.

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

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

这篇关于为什么设置document.cookie在Chrome中不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆