Skip to content
This repository has been archived by the owner on Nov 15, 2017. It is now read-only.

expires-option ... #61

Closed
ankaz opened this issue Apr 23, 2012 · 3 comments
Closed

expires-option ... #61

ankaz opened this issue Apr 23, 2012 · 3 comments

Comments

@ankaz
Copy link

ankaz commented Apr 23, 2012

... does not seem to work. I'm trying to create a cookie using

$.cookie(cookieName, cookieValue, {expires: new Date(Date.now() + (30 * 60 * 1000)), domain: vDomain, path: vPath});

But every Browser stores this cookie as session cookie. Can anyone reproduce this?

@dittodhole
Copy link

the only eye-catcher are following lines:

if (value === null) {
    options.expires = -1;
}

could you maybe isolate your breaking-call to a fiddle (eg @ jsfiddle.net)?

@bunnymatic
Copy link
Contributor

I'm seeing similar behavior - Mac OSX - Chrome 18.0.1025.163

From the console:

/** set a cookie and check it **/
> $.cookie('blah',1);
> "blah=1"
> $.cookie('blah');
> "1"
/** looks good **/

/** set a cookie with a little more data and check it **/
> $.cookie('blah', JSON.stringify({abc:1}));
> "blah=%7B%22abc%22%3A1%7D"
> $.cookie('blah');
> "{"abc":1}"
/** looks good **/

/** set the same cookie with expires **/
> $.cookie('blah', JSON.stringify({abc:1}), {expires: 7});
> "blah=%7B%22abc%22%3A1%7D; expires=Fri, 27 Apr 2012 22:45:16 GMT"
> $.cookie('blah');
> null
/** no dice! **/

Any ideas what might be happening here?

@bunnymatic
Copy link
Contributor

It looks like i had an old version. The latest code (commit 1fa4a18) seems to do the trick. The old version i was using included the following extra bit for managing expiry. This was probably the issue:

if (typeof options.expires === 'object') {
  var days = options.expires.days || 0;
  var hrs = options.expires.hours || 0;
  var mins = options.expires.minutes || 0;
  var secs = options.expires.seconds || 0;
  t = options.expires = new Date();
  t.setDate(t.getDate() + days);
  t.setHours(t.getHours() + hrs);
  t.setMinutes(t.getMinutes() + mins);
  t.setSeconds(t.getSeconds() + secs);
}

I've added a couple tests for set/get cookies with expiry. I'll submit a pull request if you feel like adding that to the project.

Cheers
Mr Rogers

@carhartl carhartl closed this as completed Jul 7, 2012
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants