How to remove cookie from a specific domain using Javascript?

Say I'm at http://www.example.com and I want to delete a cookie whose domain is .example.com and the other whose domain is www.example.com .

I am currently using this general function:

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

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

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

EDIT: Basically, I am looking for a function that can delete all cookies related to http://www.example.com , as they do not have the httponly flag. Is there such a function?

+7
javascript cookies subdomain
source share
3 answers

You can only do this if you were at http://example.com and wanted to delete the http://blah.example.com cookie. It also does not work with www.example.com - only the “base” domain can delete subdomain cookies.

There are also all-subdomains cookies that start with., And can only be deleted by the base domain.

In the base domain, this should work to remove 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'}) 
+8
source share

For security reasons, you cannot edit (or delete) cookies on another site. Since there is no guarantee that you have both foo.domain.com and bar.domain.com , you will not be able to edit the foo.domain.com cookies from bar.domain.com and vice versa.

Think about whether you were allowed to do this and went to a malicious site, then return to your bank, where you are going to deposit a check into your bank account. But, being on malicious sites, they updated your bank file with their own banking information. Now, all of a sudden, the check will be credited to the bank account of the owner of the malicious site.

+6
source share

We need to set cookies to delete them.

Example:

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

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

0
source share

All Articles