I have a page that displays data which is read from a cookie. The cookie expires after an hour. Frequently, a user will navigate to the page and keep it open. When they come back to it later, the cookie has expired and the data on the page is no longer relevant.
Can anyone suggest how I can set the cookie’s expiration time to last the length of the session?
For reference, this is the code I’m using to set the cookie (which I lifted from another project we have, and which may in turn have been lifted from this site! 🙂
function setCookie(value)
{
var today = new Date();
today.setTime(today.getTime());
var expires = 30 * 1000 * 60;
var c_name = 'CompareFundIds';
var exdate = new Date(today.getTime() + (expires));
document.cookie = c_name + "=" + value + ";expires=" + exdate.toGMTString();
}
Your headline and text seem to state two different requirements:
1) will work if you set no “expires” value at all for the cookie:
2) will work if you let your setCookie function run repeatedly as long as the page is open, say every minute:
When the browser is closed, the cookie will never be older than 1 minute and will thus be valid for another hour.