如何使用 Javascript 从特定域中删除 cookie?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17776975/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 09:35:26  来源:igfitidea点击:

How do I delete a cookie from a specific domain using Javascript?

javascriptcookiessubdomain

提问by The Random Guy

Let's say I am at http://www.example.comand I want to delete a cookie whose domain is .example.comand another one whose domain is www.example.com.

假设我在http://www.example.com并且我想删除一个域为.example.com的 cookie和另一个域为www.example.com的 cookie 。

I am currently using this generic function :

我目前正在使用这个通用函数:

var deleteCookie = function (name)
{
  document.cookie = name + '=; path=/; expires=Thu, 01 Jan 1970 00:00:01 GMT;';
};

which only seems to be removing cookies whose domain is www.example.com.

这似乎只是删除域为www.example.com 的cookie 。

But how can I specify so that it also removes cookies whose domain is .example.com?

但是我如何指定以便它也删除域为.example.com 的cookie ?

EDIT :Basically I'm looking for a function that can delete all cookies related to http://www.example.comas long as they don't have the httponly flag. Is there such a function?

编辑:基本上我正在寻找一种功能,只要它们没有 httponly 标志,就可以删除与http://www.example.com相关的所有 cookie 。有这样的功能吗?

回答by Kevin

You could do this only if you were at http://example.comand wanted to delete http://blah.example.comcookie. It wouldn't work from www.example.com either - only the "base" domain can delete subdomain cookies.

只有当您在http://example.com并想删除http://blah.example.comcookie 时,才能执行此操作。它也不适用于 www.example.com - 只有“基本”域可以删除子域 cookie。

There are also "all-subdomain" cookies, which start with a ., and can also only be deleted by the base domain.

还有“全子域”cookie,它们以 . 开头,也只能被基本域删除。

From the base domain, this should work to delete it:

从基本域中,这应该可以删除它:

document.cookie = 'my_cookie=; path=/; domain=.example.com; expires=' + new Date(0).toUTCString();

Or using the excellent jquery.cookie plugin:

或者使用优秀的 jquery.cookie 插件:

$.cookie('my_cookie',null, {domain:'.example.com'})

回答by kba

For security, you're not allowed to edit (or delete) a cookie on another site. Since there's no guarantee that you own both foo.domain.comand bar.domain.com, you won't be allowed to edit the cookies of foo.domain.comfrom bar.domain.comand vice versa.

为了安全起见,您不得编辑(或删除)其他站点上的 cookie。由于无法保证您同时拥有foo.domain.combar.domain.com,因此您将无法编辑foo.domain.comfrom的 cookie,bar.domain.com反之亦然。

Consider if you were allowed to do that and went to a malicious site, then back to your bank where you were about to deposit a cheque into your bank account. But while being on the malicious site, they updated your bank cookie with their own bank information. Now, suddenly, the cheque would be deposited into the malicious site's owner's bank account.

考虑一下是否允许您这样做并访问恶意站点,然后返回您的银行,您将在那里将支票存入您的银行帐户。但是在访问恶意站点时,他们使用自己的银行信息更新了您的银行 cookie。现在,突然之间,支票将存入恶意网站所有者的银行账户。

回答by user3198259

We need to set the cookie for delete them.

我们需要设置 cookie 来删除它们。

Example:

例子:

this.cookieService.set(cookiename, '', new Date('Thu, 01 Jan 1970 00:00:01 GMT'));

Please refer below Git location for more details. https://github.com/7leads/ngx-cookie-service/issues/5https://github.com/angular/angular/issues/9954

有关更多详细信息,请参阅下面的 Git 位置。 https://github.com/7leads/ngx-cookie-service/issues/5 https://github.com/angular/angular/issues/9954