Hot For Coding

JavaScript清除Cookie

使用JavaScript能写Cookie当然也就能清除Cookie

代码

// 琼台博客 www.qttc.net

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

使用时只要调用以下deleteAllCookies()方法即可

如果是想删除Cookie的某个字段,可以这么干

// 琼台博客 www.qttc.net

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

eraseCookie('username'); // Delete username from Cookie
分享

TITLE: JavaScript清除Cookie

LINK: https://www.qttc.net/73-javascript-clear-cookie.html

NOTE: 原创内容,转载请注明出自琼台博客