清除所有的Cookie和localStorage

归云
归云
发布于 2023-10-10 / 2800 阅读
0
0

清除所有的Cookie和localStorage

#js

清除所有的Cookie

function clearAllCookies() {
  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;path=/";
  }
}

清除所有的localStorage

function clearAllLocalStorage() {
  localStorage.clear();
}

评论