1

I'm using jQuery.cookie set cookies, however if I refresh the page or open a new page,sometimes, the cookie is lost. Why is this happening?

Here is my code:

jQuery.cookie('sid', 'sessionid', {expires: 30, path: '/', domain: 'xici.net'});

discussion: expires: 30 means 30 days, it set by jQuery.cookie library scurse code.

rainweb
  • 11
  • 1
  • 3

2 Answers2

1

Add that 30 min value to the current time to specify that exipre this cookie in 30 minutes in the future.

var date = new Date();
date.setTime(date.getTime() + (30 * 60 * 60 * 1000));
var myCookieValue = $.cookie('myCookie');
jQuery.cookie('myCookie', null);
jQuery.cookie('sid', 'sessionid', {expires: date, path: '/', domain: 'xici.net'});
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
  • Doesn't mean 30 days? From documentation: ***Create expiring cookie, 7 days from then: ***: $.cookie('the_cookie', 'the_value', { expires: 7 }); – Ionică Bizău Jul 18 '13 at 05:24
  • If you give number,it expires in that many days,what if you want to pass minutes ?? – Suresh Atta Jul 18 '13 at 05:26
  • 2
    using your solution. You pass a `Date` object. I initially posted this question to GreailsGuy, and I thought that you also say that "30 is very much in the past". – Ionică Bizău Jul 18 '13 at 06:39
  • @Johnツ No not at all :) I did'nt find any meaning in the statement "30 is very much in the past" :P – Suresh Atta Jul 18 '13 at 06:42
1

@rainweb

Try using the latest jQuery-cookie plugin and jQuery library. You are doing everything right.

Anyway to get a link to the site in question to debug it further?

Also See: https://github.com/carhartl/jquery-cookie

@suresh atta

If you give number,it expires in that many days,what if you want to pass minutes ??

By default, setting an integer value to "expires:" will be interpreted as days from time of creation or a Date object. If omitted, the cookie becomes a session cookie and gets deleted once the browsers session ends or page is closed. You cannot set an expiration time of minutes, just days or by session.

Giancarlo Colfer
  • 561
  • 5
  • 11