How do you delete all the cookies for the current domain using JavaScript?
相关问题
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- void before promise syntax
- Keeping track of variable instances
- Can php detect if javascript is on or not?
If you have access to the jquery.cookie plugin, you can erase all cookies this way:
You can get a list by looking into the document.cookie variable. Clearing them all is just a matter of looping over all of them and clearing them one by one.
I don't know why the first voted answer doesn't work for me.
As this answer said:
So my idea is to add a Cookie version control with the full set of setting, getting, removing of cookies:
Functional Approach + ES6
I found a problem in IE and Edge. Webkit browsers (Chrome, safari) seem to be more forgiving. When setting cookies, always set the "path" to something, because the default will be the page that set the cookie. So if you try to expire it on a different page without specifying the "path", the path won't match and it won't expire. The
document.cookie
value doesn't show the path or expiration for a cookie, so you can't derive where the cookie was set by looking at the value.If you need to expire cookies from different pages, save the path of the setting page in the cookie value so you can pull it out later or always append
"; path=/;"
to the cookie value. Then it will expire from any page.One liner
In case you want to paste it in quickly...
And the code for a bookmarklet :