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

编程入门 行业动态 更新时间:2024-10-28 16:26:05
本文介绍了为什么设置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中不起作用?

本文发布于:2023-10-29 17:14:38,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:中不   document   cookie   Chrome

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!