Javascript cookie 删除在 Chrome 上不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4154132/
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
Javascript cookie removal not working on Chrome
提问by Qwerky
I'm using javascript to remove a cookie but for some reason it isn't working with Chrome. The script I'm using is;
我正在使用 javascript 删除 cookie,但由于某种原因它不适用于 Chrome。我正在使用的脚本是;
function clearCookie()
{
document.cookie = 'myCookie=; expires=Thu, 01-Jan-70 00:00:01 GMT; path=/myPath/';
}
This works on;
这适用于;
- IE 8.0.6
- Firefox 3.6.12
- 浏览器 8.0.6
- 火狐 3.6.12
..but doesn't work on Chrome 7.0.517.44, after the cookie is supposed to be cleared I can still see it and the value hasn't changed.
..但是在 Chrome 7.0.517.44 上不起作用,在应该清除 cookie 之后我仍然可以看到它并且值没有改变。
Any ideas? Are there any user settings in Chrome that might prevent my cookie from being removed?
有任何想法吗?Chrome 中是否有任何用户设置可能会阻止我的 cookie 被删除?
回答by scaryman
Chrome doesn't support cookies on file:// and localhost uris. See this so question - Why does Chrome ignore local jQuery cookies?
Chrome 不支持 file:// 和 localhost uris 上的 cookie。看到这个问题 -为什么 Chrome 会忽略本地 jQuery cookie?
回答by Martin Jespersen
You need to use the right datetime format for it to work. The following should do the trick
您需要使用正确的日期时间格式才能使其工作。以下应该可以解决问题
function clearCookie()
{
document.cookie = 'myCookie=; expires='+new Date(0).toUTCString() +'; path=/myPath/';
}
And of course you need to specify the exact same path and&or domain specified on cookie creation.
当然,您需要指定与 cookie 创建时指定的完全相同的路径和/或域。
回答by deadman1204
You can clear a cookie in chrome, but you need to set the domain as well when creating the blank cookie to replace the current one.
您可以在 chrome 中清除 cookie,但在创建空白 cookie 以替换当前 cookie 时,您还需要设置域。
回答by Ismael Cruz
Chrome and FF have severity problems with this. Here you can see both browsers bugs and their status is WONTFIX...
Chrome 和 FF 在这方面存在严重问题。在这里你可以看到两个浏览器的错误,它们的状态是 WONTFIX...
Chrome: https://code.google.com/p/chromium/issues/detail?id=128513
铬:https: //code.google.com/p/chromium/issues/detail?id=128513

