Posted on

Cookie: Max-age vs Expires

The expires parameter was part of the original cookies baked up by Netscape. In HTTP version 1.1, expires was deprecated and replaced with the easier-to-use max-age—instead of having to specify a date, you can just say how long the cookie can live. By setting either of these, the cookie will persist until its time runs out, otherwise—if you set neither—the cookie will last until you close your browser (a “session cookie”).

Setting a cookie for “foo=bar” to last 5 minutes, using expires:

var d = new Date();
d.setTime(d.getTime() + 5*60*1000); // in milliseconds
document.cookie = 'foo=bar;path=/;expires='+d.toGMTString()+';';

And the same with max-age:

document.cookie = 'foo=bar;path=/;max-age='+5*60+';';

Unfortunately, none of the current versions of Internet Explorer support max-age, so if you want proper cookie persistence cross-browser, then stick to expires.

Leave a Reply

Your email address will not be published. Required fields are marked *