Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript bookmarklet to delete all cookies within a given domain

I am testing a web app that writes cookies to subdomain.thisdomain.com and several subfolders within that. I'm looking for JavaScript that I can put into a bookmarklet that will delete all cookies under that subdomain, regardless of the folder in which they exist.

Any ideas?

like image 674
Caveatrob Avatar asked Oct 07 '08 12:10

Caveatrob


People also ask

How do you clear all cookies using JS?

As this attribute is configurable*, it is possible to delete all the cookies by setting the “expiry” to any value that has already passed. The cookie property of the current document is used to modify the attributes of the cookies buy using HTML DOM cookie Property.

Can JavaScript delete cookies?

JavaScript can create, read, and delete cookies with the document.cookie property.

Can we delete Httponly cookie in JavaScript?

Note that a cookie created via HTTP with the httponly attribute cannot be deleted using the JavaScript API.


1 Answers

Derived from my answer here:

javascript:new function(){var c=document.cookie.split(";");for(var i=0;i<c.length;i++){var e=c[i].indexOf("=");var n=e>-1?c[i].substr(0,e):c[i];document.cookie=n+"=;expires=Thu, 01 Jan 1970 00:00:00 GMT";}}(); return void(0);

Due to browser security issues, this will only work when executed while on a page that has access to all the cookies you want to delete.

like image 50
Robert J. Walker Avatar answered Sep 24 '22 04:09

Robert J. Walker