How Do I Delete A Cookie From A Specific Domain Using Javascript?
Solution 1:
You could do this only if you were at http://example.com and wanted to delete http://blah.example.com cookie. It wouldn't work from www.example.com either - only the "base" domain can delete subdomain cookies.
There are also "all-subdomain" cookies, which start with a ., and can also only be deleted by the base domain.
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:
$.cookie('my_cookie',null, {domain:'.example.com'})
Solution 2:
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.com
and bar.domain.com
, you won't be allowed to edit the cookies of foo.domain.com
from bar.domain.com
and vice versa.
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.
Solution 3:
Just wanted to add to this reg. deleting top-level cookies from sub domains.
I was surfing "mysub.mysite.se" with the following script inside a referenced js-file (mysub.mysite.se/file.js).
This code did remove the _fbp cookie with domain ".mysite.se".
document.cookie = '_fbp=;expires=Thu, 01 Jan 2010 00:00:00 UTC; path=/; domain=.mysite.se';
document.cookie = '_fbp=;expires=Thu, 01 Jan 2010 00:00:00 UTC; path=/; domain=www.mysite.se';
document.cookie ='_fbp=;expires=Thu, 01 Jan 2010 00:00:00 UTC; path=/; domain=mysite.se';
So what @kba is saying is not 100% right, but this is the case when you try to remove a cookie on a 3rd party domain.
Solution 4:
We need to set the cookie for delete them.
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/5 https://github.com/angular/angular/issues/9954
Post a Comment for "How Do I Delete A Cookie From A Specific Domain Using Javascript?"