Cookie set via JS to expire when the browser closes isn't expiring

Viewed 3197

Testing in Firefox and Safari, I've tried both of the following lines. Each sets the cookie, but neither approach expires the cookie after I've closed (then re-opened) the browser. The browser's cookie info says "test" is set to expire, "At end of session", but it doesn't happen.

There are some similar posts about this (e.g, When does a cookie with expiration time 'At end of session' expire?) but nothing has helped or is about setting the the cookie via JavaScript specifically like I'm doing.

// fails to expire after browser closing
document.cookie = "test=1; expires=0; path=/";

// fails to expire after browser closing
document.cookie = "test=1; path=/";
3 Answers

"Warning: Many web browsers have a session restore feature that will save all tabs and restore them the next time the browser is used. Session cookies will also be restored, as if the browser was never closed."*

You could try using Max-Age=0

"A zero or negative number will expire the cookie immediately."*

*Source: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie

Related