views:

35

answers:

2

I have read a lot of jQuery cookie questions on here and know there is a jQuery cookie plugin (jQuery cookie). Without doing much investigation, the question: is there a way to determine expiration date of cookie?

From the jquery.cookie doc:

/**
* Get the value of a cookie with the given name.
*
* @example $.cookie('the_cookie');
* @desc Get the value of a cookie.
*
* @param String name The name of the cookie.
* @return The value of the cookie.
* @type String
*
* @name $.cookie
* @cat Plugins/Cookie
* @author Klaus Hartl/[email protected]
*/

Doesnt seem that this plugin can do it?

EDIT: the reason I want to do this is that my cookie expires after 5 minutes of inactivity and Id like to notify user that their session is about to expire from javascript.

+1  A: 

$.cookie("example", "foo", { expires: 7 });

Will expire in 7 days

Sotiris
@Sotiris I want the expiration value. I am not asking how to set the expiration value itself.
Chris
+1  A: 

Unless something has changed, you can't get this value from a cookie, you can set it but that's it, when it expires it just won't show up in the cookie collection anymore....but you can't see that it expires in 5 minutes for example.

Your best bet for something along the lines of session expiration is to use setTimeout() with the correct delay, for example if it's 5 minutes, you may want to alert at 4 min 30 seconds, like this:

setTimeout(function() {
  alert("Your session will expire in 30 seconds!");
}, 270000);  //4.5 * 60 * 1000
Nick Craver
Appreciate the answer, thanks. Since I am updating the cookie via PHP on each subsequent page request, you are recommending that I just on each page request set a timeout of cookie.expiration - (some time) to fire off a message to user? I guess I just do not understand why you can set a expiration but cannot get it back.
Chris
@Chris - I'm not sure the reasoning on that one, just know that the document/cookie API doesn't have methods to get it, if you look at `document.cookie` you'll see it isn't in there :) And yes on including the timeout on every page, or just have that timeout directly in an included JavaScript file...I would do a notification bar or something besides an alert personally, but the idea's the same :)
Nick Craver