View Full Version : How to make jQuery's cookies live after browser closes

12-19-2009, 05:16 PM

The cookie always deletes after browser closes. How do I fix it?

12-19-2009, 10:26 PM
As long as you've set the cookie expiry to a future date and the user does not have their browser clear all cookies on close then by all means it should be persistant.

To inspect the expiry I tend to use Firecookie (https://addons.mozilla.org/en-US/firefox/addon/6683) in conjunction with Firebug (https://addons.mozilla.org/en-US/firefox/addon/1843) and you can see then if the expiry is either the session or a future date.

12-20-2009, 04:45 PM
The cookie is still cleared.

jQuery.cookie = function(name, value) {
if (typeof value != 'undefined') {
document.cookie = name+'='+encodeURIComponent(value);
} else {
var cookieValue = null;
if (document.cookie && document.cookie != '') {
var cookies = document.cookie.split(';');
for (var i = 0; i < cookies.length; i++) {
var cookie = jQuery.trim(cookies[i]);
// Does this cookie string begin with the name we want?
if (cookie.substring(0, name.length + 1) == (name + '=')) {
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
return cookieValue;

Did I miss something?

12-21-2009, 10:07 AM
I don't know where you got the code above from, but from the documentation in the link you initially provided:

* @option Number|Date expires Either an integer specifying the expiration date from now on in days or a Date object. If a negative value is specified (e.g. a date in the past), the cookie will be deleted. If set to null or omitted, the cookie will be a session cookie and will not be retained when the the browser exits.

12-21-2009, 05:03 PM
I just modified their code. I now want to know how to make it survive even after browser closes.

12-22-2009, 08:21 AM
As far as I know there is no way to guarantee that the cookie won't be cleared when the browser is closed, it's all based on the browser's preference. As long as the expiry isn't set to the session then it should be retained unless otherwise setup as previously stated.