Delete cookie immediately

I looked up how to delete cookies, and most of the links recommended setting the MaxAge to some large negative number, or the expiration date to January 1970. While this certainly works, it requires the client to refresh the page to have the cookies be deleted. Is there a way to remove the cookie immediately?

Also: you can delete a cookie by pasting

document.cookie = document.cookie + "; max-age=0"

into the console.

This works:

Delete the cookie, then run this:

window.location = window.location

It should refresh their page, and they have nothing to do about it :smiley:

1 Like

Cool thanks!

It should work on any browser. You could also punish them if they don’t buy putting a invisible div, preventing them to click by putting this in a div’s styles:

position:fixed;left:0;top:0;height:100vh;width:100%;background:transparent;

then add another one prompting them to reload.

lol… uh… ok I don’t understand what this does but ok :sweat_smile:

if you put it in a div

<div style="thatcodeiput"></div>

you could punish them if they managed to ignore a redirect.

It’s possible to ignore a redirect?

I don’t think so, but it’s good to use, like in google drive when it says “Woah! this doc is huge, refresh”

Oh ok, didn’t know that - I’ll add that.

Just in case if they are on some really strong DNS and it won’t redirect (which would make for a bad one, btw) alert them or don’t allow them to click anymore.

1 Like

I searched on my browser extensions store and found an extension that deletes the cookies immediately without reloading the site. Any idea how that would work?

I found this interesting thing on W3Schools. If you set the cookie key to nothing and then the expiration date to 1970 it’ll delete without reloading.

If you’re using Chrome, to view the source of an extension, go to chrome://extensions, toggle developer mode (top right) and click unpack extension, then choose the extension you want.
If you’re not using Chrome, I don’t know how to do that :smiley:

1 Like

I’m using Opera (ftw!) and the extensions are all stored in ~/Library/Application Support/com.operasoftware.Opera/Extensions

Do you know why extensions are named like this?

Anyways the code the extension developer used was extremely complicated - I used a loop and some split() to grab all the cookie keys, set them all to "", and then appended max-age: -100 at the end so the cookie disappeared. In the end, w3schools was the most helpful as always :stuck_out_tongue_winking_eye:

I wish you could do it this way
image

1 Like