Unable to delete cookie from javascript

后端 未结 6 1194
名媛妹妹
名媛妹妹 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: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;';
    };
    

提交回复
热议问题