javascript – How to delete cookies from a website?

Question:

I have a website http://www.mysite.com that creates several cookies with different domains like: http://tracker.mysite.com and http://w3.mysite.com .

How can I use JavaScript to delete cookies from mysite domain?

Answer:

This code forces all cookies whose scope is the current site to expire:

function apagaCookies() {
    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";
    }
}

Source: Original post on OS in English .

Scroll to Top