i’m using the jquery cookie plugin. Everything works fine except the fact that I have no idea how to set an expiration-time for the cookie?
$.cookie('opt_visible', 'true');
the jquery-cookie documentation says:
hoursToLive (DEPRECATED for expiresAt)
- NUMBER
- For how many hours should the cookie be valid? (Passing 0 means to delete the cookie at the end of the browser session–this is default. Negative values will delete the cookie, but you should use the del() method instead.)
That’s exactly what I’d like to have. The cookie should be available as long as i’m browsing the site. As soon as i close the window or browsertab, the cookie should be deleted.
How can i implement this hoursToLive thingy to my mentioned line above?
Thank you
To my understanding you don’t need to do anything at all.
hoursToLiveis deprecated but not removed yet. As 0 is the default forhoursToLiveanyway the cookie will be removed when the browser is closed (!not when only the tab is closed but the browser remains open).If you really need to remove the cookie when the tab is closed you could try attaching an event handler to the window unload event
jQuery(window).unload(...)and inside that handler call thedel('...')but that might not always workIf it’s enough that the cookie expires after e.g. 1 hour you can simply pass in the
expiresAtoption when setting the cookie