Reputation: 797
I was able to set a cookie using jQuery with a redirect to a landing page but I have no idea how can I set the cookie expiry date to less then one day (for example 15 minutes). I was not able to find explanation in the plugin documentation
Here is my code:
$(function() {
var COOKIE_NAME = 'splash-page-cookie';
$go = $.cookie(COOKIE_NAME);
if ($go == null) {
$.cookie(COOKIE_NAME, 'test', { path: '/', expires: 1 });
window.location = "/example"
}
else {
// do nothing
}
});
Thank you for all help !
EDIT: I was able to set cookie as session cookie by skipping expire, well im fine with that but its not perfect, if you guys have some idea i would be grateful.
Upvotes: 6
Views: 27917
Reputation: 321
A fraction of a day should work, so for example there are 1440 minutes in a day so if you wanted the cookie to expire in 15 minutes you could simply divide the minutes by 1440 like so:
$.cookie('foo', 'bar', {expires: 15/1440});
This would also work with hours, so for example if you wanted the cookie to expire in 2 hours you could do:
$.cookie('foo', 'bar', {expires: 2/24});
A third option is to pass a date object like so:
var expire = new Date();
//set expiry to current time plus 15 minutes in milliseconds
expire.setTime(expire.getTime() + (15 * 60 * 1000));
$.cookie('foo', 'bar', {expires: expire});
Hope this helps!
Upvotes: 10
Reputation: 318182
The plugins expires
option accepts either a number or a date object.
If a number is passed, it's the number of days until the cookie expires, but if a date object is passed, it's the time and date when the cookie expires, so you can do
var expDate = new Date();
expDate.setTime(expDate.getTime() + (15 * 60 * 1000)); // add 15 minutes
$.cookie(COOKIE_NAME, 'test', { path: '/', expires: expDate });
Upvotes: 12