search

Home  >  Q&A  >  body text

javascript - How to clear cookies

document.cookie = key=''; expires=${new Date(new Date().getTime() new Date().setTime(-1 * 1000 * 3600 * 24)).toUTCString( )}
I checked and it said to set the expiration time, but I tried but failed

習慣沉默習慣沉默2768 days ago792

reply all(3)I'll reply

  • 给我你的怀抱

    给我你的怀抱2017-05-24 11:33:56

    This is a problem for which there is a mature solution.

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

    reply
    0
  • 滿天的星座

    滿天的星座2017-05-24 11:33:56

    The answer above is correct. If it cannot be cleared, it may be set to not allow client operations

    reply
    0
  • PHP中文网

    PHP中文网2017-05-24 11:33:56

    The server sets a new cookie with an expiration date earlier than now.

    reply
    0
  • Cancelreply