Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 339
Adam K Dean Avatar asked Oct 01 '13 13:10

Adam K Dean


People also ask

Do session cookies expire?

Session cookies expire once you log off or close the browser. They are only stored temporarily and are destroyed after leaving the page. They are also known as transient cookies, non-persistent cookies, or temporary cookies.

How do cookies expire after session?

To set a cookie so it expires at the end of the browsing session, simply OMIT the expiration parameter altogether. Save this answer.

What happen if cookie expires max age is session?

Cookies without an Expires or Max-Age attribute are treated as session cookies, which means they are removed once the browser is closed. Setting a value on either Expires or Max-Age makes them permanent cookies, since they will exist until they hit their expiry date.

How long are session cookies stored?

In general session cookies disappear once the user closes the browser. All other permanent cookies have an expiration date written in their code. The cookies should last no longer than 12 months, but in practice, many cookies have a much longer duration.


3 Answers

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!

like image 152
Adam K Dean Avatar answered Nov 02 '22 03:11

Adam K Dean


It's possible in the RFC 2965 specification. That said it would be possible if you set the "Discard" property in your cookie handling code and if the browser supports it.

That said: jQuery cookie doesn't support the Discard-property. You would have to extend the jQuery-Cookie code like so (it's about line 57 in the original file):

        return (document.cookie = [
            config.raw ? key : encodeURIComponent(key),
            '=',
            config.raw ? value : encodeURIComponent(value),
            options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
            options.path    ? '; path=' + options.path : '',
            options.domain  ? '; domain=' + options.domain : '',
                            options.discard ? '; Discard=' + options.discard : '',
            options.secure  ? '; secure' : ''
        ].join(''));
like image 45
schlingel Avatar answered Nov 02 '22 03:11

schlingel


if you use a default setup like this

 $.cookie.defaults = { path: '/', expires: 365 };

then you need to use the following attribute for each cookie value

$.cookie(key, value, { expires: null });
like image 34
Otto Kanellis Avatar answered Nov 02 '22 04:11

Otto Kanellis