How to delete a cookie? How to delete a cookie? javascript javascript

How to delete a cookie?


Try this:

function delete_cookie( name, path, domain ) {  if( get_cookie( name ) ) {    document.cookie = name + "=" +      ((path) ? ";path="+path:"")+      ((domain)?";domain="+domain:"") +      ";expires=Thu, 01 Jan 1970 00:00:01 GMT";  }}

You can define get_cookie() like this:

function get_cookie(name){    return document.cookie.split(';').some(c => {        return c.trim().startsWith(name + '=');    });}


Here a good link on Quirksmode.

function setCookie(name,value,days) {    var expires = "";    if (days) {        var date = new Date();        date.setTime(date.getTime() + (days*24*60*60*1000));        expires = "; expires=" + date.toUTCString();    }    document.cookie = name + "=" + (value || "")  + expires + "; path=/";}function getCookie(name) {    var nameEQ = name + "=";    var ca = document.cookie.split(';');    for(var i=0;i < ca.length;i++) {        var c = ca[i];        while (c.charAt(0)==' ') c = c.substring(1,c.length);        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);    }    return null;}function eraseCookie(name) {       document.cookie = name+'=; Max-Age=-99999999;';  }


would this work?

function eraseCookie(name) {    document.cookie = name + '=; Max-Age=0'}

I know Max-Age causes the cookie to be a session cookie in IE when creating the cookie. Not sure how it works when deleting cookies.