Javascript – How to remove a cookie by using Javascript

cookies, javascript

How to remove the cookie set by


The following statement doesn't work.

javascript:void(document.cookie=”PREF=ID=20b6e4c2f44943bb:U=4bf292d46faad806:TM=1249677602:LM=1257919388:S=odm0Ys-53ZueXfZG;expires=Thu, 01 Jan 1970 00:00:00 GMT;path=/;”);

What's wrong with the removal code?

Best Solution

Trick is right... in particular you need to put any past-value in the expires header. (These days you'd use a full-year, though; the two-digit format goes back to the early Netscapes only.)

Also ensure you don't use smart quotes like in your quote above.

javascript:alert(document.cookie='PREF=X;path=/;;expires=Sat, 01-Jan-2000 00:00:00 GMT');

Note that the format produced by Date.toGMTString is not the same as the date format required by the cookie specification, although it does still work in many browsers.