JavaScript清除Cookie

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

代码

【爆款云服务器限时促销】
阿里云云服务器ECS实例2核2G,新人专享渠道特惠价只要82元!特惠热卖中。 点击立即购买

// 琼台博客 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: 原创内容,转载请注明出自琼台博客