How to remove a cookie by using Javascript?

2020-04-01 08:19发布

How to remove the cookie set by

javascript:void(document.cookie=”PREF=ID=20b6e4c2f44943bb:U=4bf292d46faad806:TM=1249677602:LM=1257919388:S=odm0Ys-53ZueXfZG;path=/; domain=.google.com”);

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=/; domain=.google.com”);

What's wrong with the removal code?

5条回答
闹够了就滚
2楼-- · 2020-04-01 08:38

Your cookie domain is .google.com, if you're not actually running the code from that domain you will not be able to modify the cookie.

查看更多
Deceive 欺骗
3楼-- · 2020-04-01 08:52

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=/;domain=.google.com;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.

查看更多
等我变得足够好
4楼-- · 2020-04-01 08:57

Agreed, @bobince. The official documentation says to use Date.toUTCString() for cookie expiration dates.

I am 95% sure that you must set an expiration date when creating the cookie crumb if you want to force its removal later. A cookie crumb created without an explicit expiration date is a session cookie (crumb) by default, which means that it is not removed until the browser is closed. I recall trying to expire a session cookie to no avail, in the past.

If you do set an expiration date on the cookie crumb in the first place, remember that you can use a variable for the new expiration date.

// assuming a non-session cookie crumb called "someCrumbName" exists:
var now = new Date();
var expirationDate = new Date();
var someValue = "foo";

// set the expiration date to a week ago and delete the cookie
expirationDate.setDate(now.getDate() - 7);
document.cookie = "someCrumbName=" + someValue + ";expires=" + expirationDate.toUTCString();
查看更多
可以哭但决不认输i
5楼-- · 2020-04-01 09:01

Why don't you just stick with one question or figure it out for yourself rather than keep posting your problems every few minutes?

e.g. https://stackoverflow.com/questions/1802210/how-to-recover-google-classic-design-from-its-new-design

How to reverse the effect of the following execution by using Javascript?

查看更多
在下西门庆
6楼-- · 2020-04-01 09:03
Thu, 01-Jan-70 00:00:01 GMT

Set time one second after midnight

查看更多
登录 后发表回答