如何删除当前域设置的所有 cookie?

问题描述

我的 Javascript 正在 https://somewhere.com 上触发

https://somewhere.com 设置了一堆 cookie:

  1. 所有这些 cookie 都有 Path=/
  2. 一些这些 cookie 具有 Domain=somewhere.com
  3. 其余这些 cookie 有 Domain=.somewhere.com

我想删除所有上述 cookie,为此我执行以下操作:

var allCookies = document.cookie.split(';'); 
      
for (var i = 0; i < allCookies.length; i++) {
  var cookie = allCookies[i];
  var eqPos = cookie.indexOf("=");
  var name = eqPos > -1 ? cookie.substr(0,eqPos) : cookie;
  document.cookie = name + "=;Path=/;expires=Thu,01 Jan 1970 00:00:00 GMT";
  document.cookie = name + "=;Domain=somewhere.com;Path=/;expires=Thu,01 Jan 1970 00:00:00 GMT";
  document.cookie = name + "=;Domain=.somewhere.com;Path=/;expires=Thu,01 Jan 1970 00:00:00 GMT";
}

执行上述操作后,我刷新页面并在 Chrome 开发人员工具上检查应用程序 > Cookies 并看到 一些 cookie 确实已被删除,但其他一些 cookie(在 Domain=somewhere 下。 com Domain=.somewhere.com) 存在。

我如何确定上面的脚本确实确实删除所有https://somewhere.com 创建的 cookie 以及我在之后看到的 cookie 我刷新只是在刷新后重新添加在这种情况下这很好,因为它有意义)?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)