如何使用JavaScript删除当前域的所有cookie ?


当前回答

这是一个简单的代码删除所有的JavaScript cookie。

function deleteAllCookies(){
   var cookies = document.cookie.split(";");
   for (var i = 0; i < cookies.length; i++)
     deleteCookie(cookies[i].split("=")[0]);
}

function setCookie(name, value, expirydays) {
 var d = new Date();
 d.setTime(d.getTime() + (expirydays*24*60*60*1000));
 var expires = "expires="+ d.toUTCString();
 document.cookie = name + "=" + value + "; " + expires;
}

function deleteCookie(name){
  setCookie(name,"",-1);
}

执行deleteAllCookies()函数清除所有cookie。

其他回答

这是一个简单的代码删除所有的JavaScript cookie。

function deleteAllCookies(){
   var cookies = document.cookie.split(";");
   for (var i = 0; i < cookies.length; i++)
     deleteCookie(cookies[i].split("=")[0]);
}

function setCookie(name, value, expirydays) {
 var d = new Date();
 d.setTime(d.getTime() + (expirydays*24*60*60*1000));
 var expires = "expires="+ d.toUTCString();
 document.cookie = name + "=" + value + "; " + expires;
}

function deleteCookie(name){
  setCookie(name,"",-1);
}

执行deleteAllCookies()函数清除所有cookie。

这对我来说很管用:

function deleteCookie(name) {
  document.cookie = 
    `${name}=; Expires=Thu, 01 Jan 1970 00:00:01 GMT; Path=/`;

    // Remove it from local storage too
    window.localStorage.removeItem(name);
}

function deleteAllCookies() {
  const cookies = document.cookie.split("; ");

  cookies.forEach((cookie) => {
    const name = cookie.split("=").shift();
    this.deleteCookie(name);
  });

  // Some sites backup cookies in `localStorage`
  window.localStorage.clear();
}

下面的代码将删除当前域内的所有cookie和所有尾随子域(www.some.sub.domain.example, .some.sub.domain。例如,.sub.domain。例如,等等。)

一个单行的香草JS版本(我认为这里唯一一个没有使用cookie.split()):

document.cookie.replace(/(?<=^|;).+?(?=\=|;|$)/g, name => location.hostname.split('.').reverse().reduce(domain => (domain=domain.replace(/^\.?[^.]+/, ''),document.cookie=`${name}=;max-age=0;path=/;domain=${domain}`,domain), location.hostname));

这是这一行的一个可读版本:

document.cookie.replace(
  /(?<=^|;).+?(?=\=|;|$)/g,
  name => location.hostname
    .split(/\.(?=[^\.]+\.)/)
    .reduceRight((acc, val, i, arr) => i ? arr[i]='.'+val+acc : (arr[i]='', arr), '')
    .map(domain => document.cookie=`${name}=;max-age=0;path=/;domain=${domain}`)
);

我在这里贡献是因为这个功能将允许您删除所有cookie(匹配路径,默认为no-path或\),也包括设置为包含在子域上的cookie

function clearCookies( wildcardDomain=false, primaryDomain=true, path=null ){
  pathSegment = path ? '; path=' + path : ''
  expSegment = "=;expires=Thu, 01 Jan 1970 00:00:00 GMT"
  document.cookie.split(';').forEach(
    function(c) { 
      primaryDomain && (document.cookie = c.replace(/^ +/, "").replace(/=.*/, expSegment + pathSegment))
      wildcardDomain && (document.cookie = c.replace(/^ +/, "").replace(/=.*/, expSegment + pathSegment + '; domain=' + document.domain))
    }
  )
} 

如果你只关心清除安全源上的Cookie,你可以使用Cookie Store API和它的.delete()方法。

cookieStore.getAll().then(cookies => cookies.forEach(cookie => {
    console.log('Cookie deleted:', cookie);
    cookieStore.delete(cookie.name);
}));

访问caniuse.com表,查看Cookie Store API的浏览器支持情况。