Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery cookie persistence

Tags:

jquery

cookies

I am using this jquery plugin to successfully add cookies to a jQuery Ajax application on a website.

But, my very short question is, how can I set the cookie to exist indefinately?

I am trying to remember users preferences settings by storing them long term in a cookie containing an array of data unique to each user.

Thank you.

like image 976
sisko Avatar asked Feb 27 '12 14:02

sisko


2 Answers

You can't set a cookie to exist indefinitely. You make a cookie persistent by setting an expiration date on it, and as the expiration date has to have a specific value, that's when the cookie will expire.

You can make the cookie live for a year, for example:

$.cookie('the_cookie', 'the_value', { expires: 365 });

You can also use a Date object for the expiration date, but you should keep the date on this side of year 2038, as some systems doesn't support dates after that yet.

like image 89
Guffa Avatar answered Sep 19 '22 14:09

Guffa


You can't set a cookie to "never" expire, but you can do just as good.

Usually I prefer to set cookies for 30 days, and renew them whenever the user accesses a page. As long as the user doesn't leave for a month, the cookie is permanent.

like image 34
Niet the Dark Absol Avatar answered Sep 19 '22 14:09

Niet the Dark Absol