开发者

Unable to delete cookie from javascript

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

which is supposed to set the document cookie to expire in 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


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.

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.

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.

Let me know if that helps.


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".

To fix I added the cookie domain to the set value

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.


I had a similar issue when trying to remove certain cookies. Sometimes this worked:

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:

.mydoamin.com
sub.mydomain.com 

So, my solution was to make a generic function that removes the cookie from all domains.

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;';
};


For me the issue was that I was setting the domain field which is required only if you overrode it when setting the cookie. Therefore, the following should do the trick:

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


Clear session cookies in ie11?

May be The Link above can give a Help

Just run the JavaScript like Below

document.execCommand("ClearAuthenticationCache")

I tried and The cookie was cleared.


What helped me was that you need to delete the cookie in the same state as you created it.

If you created it with a path / and a domain mydomain.com, then you have to delete it with those.

If you did not use path or domain when creating the cookie, you can not have the path or domain when deleting it.

Trick was to delete it in the EXACT same way it was created.

Hope this helps more people, I went crazy on this one even though I used the right path and domain..


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:

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.


Recently ran into this issue, thought would share my solution here.

I have been using the following to reset cookies.

 document.cookie = 'testcookie=; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/;domain=domain;'

Now this was working for some cookies but for some it was not. On debugging, I found that those cookies for which this failed was set without a domain.

So if the cookie was set with domain, you need domain in the clear request and if cookie was not set with domain you should omit it.

Anyway I ended up doing the below to clear cookies irrespective of the case.

 document.cookie = 'testcookie=; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/;domain=domain;'
 document.cookie = 'testcookie=; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/;domain=;'

This behaviour is similar for both Chrome and Firefox.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜