Hot For Coding
JavaScript清除Cookie

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

代码

// Nicholas Lee 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的某个字段,可以这么干

// Nicholas Lee 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: 转载内容请注明出处