Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery Cookies Set Expire Time

Tags:

jquery

cookies

I have this:

if (typeof options.expires == 'number') {
date = new Date();
date.setTime(date.getTime() + (options.expires * 5 * 1000));
} else {
date = options.expires;
}
expires = '; expires=' + date.toUTCString();

How to make cookies expire after 10 minutes?

like image 880
Евгений Антипов Avatar asked Feb 18 '13 15:02

Евгений Антипов


People also ask

How do I set cookies to expire time?

You can extend the life of a cookie beyond the current browser session by setting an expiration date and saving the expiry date within the cookie. This can be done by setting the 'expires' attribute to a date and time.

How do you set cookies to expire in 30 days?

Or you might use mktime(). time()+60*60*24*30 will set the cookie to expire in 30 days. If not set, the cookie will expire at the end of the session (when the browser closes). The path on the server in which the cookie will be available on.

How do I set cookies for 1 minute?

Solution 1 Specifically, time() - 60 is one minute ago, not one minute to come. Try time() + 60 and it will expire in one minute's time ...


2 Answers

 var date = new Date();
 var m = 10;
 date.setTime(date.getTime() + (m * 60 * 1000));
 $.cookie("cookie", "value", { expires: date });

Alternatively you could use a function:

function ExpireCookie(minutes) {
 var date = new Date();
 var m = minutes;
 date.setTime(date.getTime() + (m * 60 * 1000));
 $.cookie("cookie", "value", { expires: date });
}

Then do ExpireCookie(10);

like image 54
Darren Avatar answered Oct 09 '22 14:10

Darren


10 minutes is 10 * 60 * 1000 milliseconds.

var date = new Date();
date.setTime(date.getTime() + (10 * 60 * 1000));
$.cookie("example", "foo", { expires: date });
like image 23
coder Avatar answered Oct 09 '22 14:10

coder