Html 在 Chrome 控制台中修改 document.cookie 不起作用

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

Modify document.cookie in Chrome console not working

javascripthtmlcookies

提问by bigbearzhu

Can I modify document.cookie in the console of Chrome Developer Tools?

我可以在 Chrome 开发者工具的控制台中修改 document.cookie 吗?

My current cookie string was like:

我当前的 cookie 字符串是这样的:

"coldcookie="

It seems it just doesn't work if I run this code below:

如果我在下面运行此代码,它似乎不起作用:

document.cookie = document.cookie + "; newcookie=something"

The document.cookie wouldn't change at all.

document.cookie 根本不会改变。

Update: I found that if I run:

更新:我发现如果我运行:

document.cookie = "newcookie"

It actually add a "newcookie" in the cookie string like:

它实际上在 cookie 字符串中添加了一个“newcookie”,例如:

"oldcookie=; newcookie"

Shouldn't that clear the current cookie string?

这不应该清除当前的 cookie 字符串吗?

It does the same thing in IE. So I think there must be some rule there. Any ideas?

它在 IE 中做同样的事情。所以我认为那里必须有一些规则。有任何想法吗?

采纳答案by Seth

Cookies are set to expire, since we can't really "delete" them, we just force them to expire with a past date.

Cookie 设置为过期,因为我们无法真正“删除”它们,我们只是强制它们在过去的日期过期。

function deleteCookie(name) {
    document.cookie = name + '=;expires=Thu, 05 Oct 1990 00:00:01 GMT;';
};

deleteCookie('newcookie')