19

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?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Caveatrob
  • 12,667
  • 32
  • 107
  • 187
  • Very good for create a cookieless subdomain (I mean very good to serve static resources according to Google guidelines: http://code.google.com/intl/fr/speed/page-speed/docs/caching.html) – Thomas Decaux Oct 12 '10 at 19:06
  • Does this answer your question? [Clearing all cookies with JavaScript](https://stackoverflow.com/questions/179355/clearing-all-cookies-with-javascript) – LWC Oct 11 '20 at 05:44

2 Answers2

25

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.

guerda
  • 23,388
  • 27
  • 97
  • 146
Robert J. Walker
  • 10,027
  • 5
  • 46
  • 65
  • Is there a place to put the domain name in this JavaScript? I'd like to remove cookies within my own browser for a particular domain and all subdomains. Like "www.microsoft.com" would take anything that starts with "www.microsoft.com" and remove all cookies. – Caveatrob Oct 07 '08 at 19:38
  • 1
    Bookmarklets play inside the browser's security sandbox, so they're not allowed to edit cookies from a different domain. – Robert J. Walker Oct 07 '08 at 21:08
  • Okay. I didn't read that right. I figured anything running as a bookmarklet would have access to all browser settings. – Caveatrob Oct 09 '08 at 14:55
  • Used the JS in a page to remove all cookies for a site. Worked great. Thx! – Walden Leverich Mar 15 '10 at 18:39
  • What's with the `new function(){}`? – Hello71 Feb 13 '11 at 00:59
  • 1
    It creates an anonymous function which ensures that the variables inside it are scoped to be within that function only. – Robert J. Walker Feb 13 '11 at 03:09
4

I would recommend Firecookie extension to Firebug.

Sergey Ilinsky
  • 31,255
  • 9
  • 54
  • 56