jscheuer1
11-19-2005, 11:10 PM
I recently made another foray into using cookies and discovered that there is generally (at least as far as I could tell) no way to query document.cookie and find out when a specific value expires. I devised a way to do this and thought folks might be interested. It builds on the cookie unit (included here for clarity) found on Quirksmode dot org (http://www.quirksmode.org). It relies on the fact that, since you set the cookie value in the first place, there is no reason why you cannot set another value which contains its expiration and query that value later to find out how much time is left (I used days as a reasonable time period to express things in, other time units could be used, if preferred):
//Begin http://www.quirksmode.org Cookie Code:
function createCookie(name,value,days) //modified by jscheuer1 to store expire date (if any) in retrievable form and later calculate days remaining
{
if (days)
{
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
var setAt=Date.parse(date.toGMTString()) // new
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
if (days) // these two lines new
document.cookie = name+"set="+setAt+expires+"; path=/";
}
function readCookie(name)
{
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++)
{
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}
function dateCookie(name) //Expires in ? days function added - jscheuer1
{
var nameEQ = name + "set=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++)
{
var date=new Date()
date=Date.parse(date.toGMTString())
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return Math.ceil((Math.abs(c.substring(nameEQ.length,c.length))-date)/(24*60*60*1000));
}
return null;
}
function eraseCookie(name)
{
createCookie(name,"",-1);
}
//End http://www.quirksmode.org Cookie Code
The dateCookie function accepts the cookie name as a parameter and returns the time (rounded up to the nearest amount of days) left before the cookie expires, if the cookie exists and is that type otherwise, null is returned.
//Begin http://www.quirksmode.org Cookie Code:
function createCookie(name,value,days) //modified by jscheuer1 to store expire date (if any) in retrievable form and later calculate days remaining
{
if (days)
{
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
var setAt=Date.parse(date.toGMTString()) // new
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
if (days) // these two lines new
document.cookie = name+"set="+setAt+expires+"; path=/";
}
function readCookie(name)
{
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++)
{
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}
function dateCookie(name) //Expires in ? days function added - jscheuer1
{
var nameEQ = name + "set=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++)
{
var date=new Date()
date=Date.parse(date.toGMTString())
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return Math.ceil((Math.abs(c.substring(nameEQ.length,c.length))-date)/(24*60*60*1000));
}
return null;
}
function eraseCookie(name)
{
createCookie(name,"",-1);
}
//End http://www.quirksmode.org Cookie Code
The dateCookie function accepts the cookie name as a parameter and returns the time (rounded up to the nearest amount of days) left before the cookie expires, if the cookie exists and is that type otherwise, null is returned.