1

I want to delete all the cookie key as well as value. I was using this code and It only delete value. This code only set the cookie empty .I want to delete all cookies key as well as value.

function deleteAllCookies() {
    var cookie = 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";
    }
}
Nishant
  • 466
  • 2
  • 15
Sharad kumar
  • 187
  • 2
  • 14

1 Answers1

0

The only way in Javascript to mark a cookie as deleted is to empty its value and set its expiration to a date in the past, just as you do with your code. The web browser handles the rest and deletes the cookie. So, although it seems that you are only emptying the value and changing the expiration date the cookie is actually deleted.

You can verify this by observing the list of cookies in the browser's dev console while running your deleteAllCookies function.

Befeepilf
  • 617
  • 4
  • 10