无法从javascript中删除Cookie [英] Unable to delete cookie from javascript

查看:152
本文介绍了无法从javascript中删除Cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在外部网站上,并尝试通过javascript删除Cookie。

I am on an external site, and I am trying to delete the cookie via javascript.

我在控制台中执行了以下操作:

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

它应该设置文档cookie在1970年到期

which is supposed to set the document cookie to expire in 1970

但之后,我调用

document.cookie.split(";")

未触动过。任何想法为什么?

The cookies are seemingly untouched. Any ideas why?

PS:上面的代码来自stackoverflow
使用javascript清除所有Cookie

PS: code above is from stackoverflow Clearing all cookies with javascript

推荐答案

我遇到了这个问题,解决了。您的Cookie很可能未被删除,因为当您设置新值时,它必须与您尝试删除的原始Cookie的路径和域名相匹配。

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.

换句话说:

 document.cookie = name + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT;path=[something];"

something值需要与现有Cookie设置的值一致。

that "something" value needs to line up with whatever the existing cookies have set.

JS调试器可能不会提供有关路径和域是什么的详细信息,但是如果您查找现有Cookie的值,您的Chrome->设置或Firefox / Safari / IE中的类似面板。

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.

让我知道如果有帮助。

这篇关于无法从javascript中删除Cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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