Is it possible for jQuery cookies to expire like session variables?

Adam K Dean picture Adam K Dean · Oct 1, 2013 · Viewed 23.8k times · Source

I know this question has been asked a thousand times but none of the answers really give me what I'm looking for. I am using jQuery cookie to store some information, but I want them to expire when the browser closes. window.unload is not a viable option (read: doesn't work.)

My question is this: is it actually possible to have the cookies set to expire when the browser closes, like a session? If so, does anyone know how?

Update: Turns out it's quite simple, instead of passing expiry: 0 (which didn't work), just don't pass any expiry at all. Well, this explains why there wasn't many questions.

Thanks all.

Answer

Adam K Dean picture Adam K Dean · Oct 1, 2013

Turns out it's quite simple, instead of passing expiry: 0 (which didn't work), just don't pass any expiry at all. Well, this explains why there wasn't many questions.

So instead of passing expiry with the flags like so:

$.cookie(key, value, { expiry: 0, domain: '', path: '' });

You just omit the expiry value:

$.cookie(key, value, { domain: '', path: '' });

And it works. It's such a simple thing, that it confused me.

But thanks anyway!