Unable to delete cookie from javascript

后端 未结 6 1146
名媛妹妹
名媛妹妹 2021-01-01 09:35

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


        
相关标签:
6条回答
  • 2021-01-01 10:04

    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.

    0 讨论(0)
  • 2021-01-01 10:06

    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.

    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.

    0 讨论(0)
  • 2021-01-01 10:10

    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.

    0 讨论(0)
  • 2021-01-01 10:10

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

    0 讨论(0)
  • 2021-01-01 10:18

    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.

    0 讨论(0)
  • 2021-01-01 10:23

    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=/"
    
    0 讨论(0)
提交回复
热议问题