无法从 javascript 中删除 cookie
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5688491/
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
Unable to delete cookie from javascript
提问by ming yeow
I am on an external site, and I am trying to delete the cookie via javascript.
我在一个外部站点上,我正在尝试通过 javascript 删除 cookie。
I did the following in the console:
我在控制台中执行了以下操作:
function deleteAllCookies() {
var cookies = document.cookie.split(";");
for (var i = 0; i < cookies.length; i++) {
var cookie = cookies[i];
var eqPos = cookie.indexOf("=");
var name = eqPos > -1 ? cookie.substr(0, eqPos) : cookie;
document.cookie = name + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT";
}
}
deleteAllCookies()
which is supposed to set the document cookie to expire in 1970
应该将文档 cookie 设置为在 1970 年到期
But after that, I call
但在那之后,我打电话
document.cookie.split(";")
The cookies are seemingly untouched. Any ideas why?
饼干似乎没有受到影响。任何想法为什么?
PS: code above is from stackoverflow Clearing all cookies with JavaScript
PS:上面的代码来自 stackoverflow 使用 JavaScript 清除所有 cookie
回答by Stan Lin
I just ran into this issue and finally solved it. Your cookie is most likely not being deleted because when you set the new value, it has to match the path and domain of the original cookie you're trying to delete.
我刚遇到这个问题,终于解决了。您的 cookie 很可能不会被删除,因为当您设置新值时,它必须与您尝试删除的原始 cookie 的路径和域相匹配。
In other words:
换句话说:
document.cookie = name + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT;path=[something];"
that "something" value needs to line up with whatever the existing cookies have set.
“某物”值需要与现有 cookie 设置的任何内容保持一致。
JS debuggers might not give you details on what the path and domain are, but it will become obvious which one you're not matching on if you look up the value of the existing cookie in your Chrome->settings or similar panel in Firefox/Safari/IE.
JS 调试器可能不会为您提供有关路径和域的详细信息,但是如果您在 Chrome->settings 或 Firefox 中的类似面板中查找现有 cookie 的值,则很明显您不匹配哪个Safari/IE。
Let me know if that helps.
如果这有帮助,请告诉我。
回答by WearyMonkey
I had the same issue. I discovered that the cookie was set under an empty subdomain, e.g. the cookie domain was ".domain.com", and my website was hosted at "sub.domain.com".
我遇到过同样的问题。我发现 cookie 被设置在一个空的子域下,例如 cookie 域是“.domain.com”,而我的网站托管在“sub.domain.com”。
To fix I added the cookie domain to the set value
为了修复我将 cookie 域添加到设置值
document.cookie = name + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT; domain=.domain.com";
To see what domain the cookie is set to, in Chrome, open dev tools -> resources -> cookies and look at the domain fields.
要查看 cookie 设置的域,在 Chrome 中,打开开发工具 -> 资源 -> cookie 并查看域字段。
回答by Ponyboy
I had a similar issue when trying to remove certain cookies. Sometimes this worked:
我在尝试删除某些 cookie 时遇到了类似的问题。有时这有效:
document.cookie = name + '=;expires=Thu, 01 Jan 1970 00:00:01 GMT;path=/;';
document.cookie = name + '=;expires=Thu, 01 Jan 1970 00:00:01 GMT;path=/;';
...and sometimes it didn't.
......有时它没有。
After looking into the Chrome Inspector (Application tab -> Storage sidebar -> Cookies) I noticed that some cookies were set with different domains. Example:
在查看 Chrome Inspector(应用程序选项卡 -> 存储侧边栏 -> Cookies)后,我注意到一些 cookie 设置了不同的域。例子:
.mydoamin.com
sub.mydomain.com
So, my solution was to make a generic function that removes the cookie from all domains.
所以,我的解决方案是制作一个通用函数,从所有域中删除 cookie。
var deleteCookie = function(name) {
document.cookie = name + '=;expires=Thu, 01 Jan 1970 00:00:01 GMT;path=/;domain=.mydomain.com;';
document.cookie = name + '=;expires=Thu, 01 Jan 1970 00:00:01 GMT;path=/;domain=sub.mydomain.com;';
};
回答by Bin Liu
Clear session cookies in ie11?
May be The Link above can give a Help
可能是上面的链接可以提供帮助
Just run the JavaScript like Below
只需像下面一样运行 JavaScript
document.execCommand("ClearAuthenticationCache")
I tried and The cookie was cleared.
我试过了,cookie 被清除了。
回答by Nicholas K
For me the issue was that I was setting the domain
field which is required only if you overrodeit when setting the cookie. Therefore, the following should do the trick:
对我来说,问题是我设置了只有在设置 cookie 时覆盖它时才domain
需要的字段。因此,以下应该可以解决问题:
document.cookie = name + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/"
回答by Mohammad Baygi
I was working on a browser bookmarklet to remove cookies from the current domain, I had the same issue, my issue was that I was not using domain either. Here is my bookmarklet value eventually:
我正在使用浏览器书签从当前域中删除 cookie,我遇到了同样的问题,我的问题是我也没有使用域。这是我最终的书签价值:
javascript: (function(){document.cookie.split(";").forEach(function(c) { document.cookie = c.replace(/^ +/, "").replace(/=.*/, "=;expires=" + new Date().toUTCString() + ";domain=." + location.host.split('.').slice(-2).join(".") +";path=/"); }); })();
Note that I replace "domain.com" with location.host.split('.').slice(-2).join(".") so that I always get the domain name without subdemains, i.e. mail.google.com would become google.com. when setting cookie expiry we should ignore the subdemain (at least in my case it was the case.
请注意,我将“domain.com”替换为 location.host.split('.').slice(-2).join(".") 以便我始终获得没有子域名的域名,即 mail.google.com将成为 google.com。在设置 cookie 过期时,我们应该忽略 subdemain(至少在我的情况下是这样。