Setting an Expiration Date


document.cookie="myLanguage=JavaScript; expires=Tue, 25-Dec-2007 12:34:56 GMT" 

By default, cookies expire when the user closes the browser. These cookies are then called temporary cookies. The opposite are persistent cookies, meaning cookies that live longer than the browser session. So when the browser is restarted, the cookie is still there.

For this to work, the cookie needs an expiration date detailing how long the cookie may remain in the browser. The cookie specification demands that the expiration date is provided in GMT (Greenwich mean time), and that the following format is used:

Wdy, DD-Mon-YYYY HH:MM:SS GMT 


Here is a possible value:

Tue, 25-Dec-2007 12:34:56 GMT 


This makes determining the correct date information (especially the weekday) quite hard; eventually, external libraries that can help with this task come in handy.

When the date is determined, it just has to be used in the value for document.cookie, as the code at the beginning of this phrase has demonstrated (see expirecookie.html).

Figure 7.3 shows the output of this code on a system that uses the GMT+1 time zone and therefore is one hour ahead of GMT. Of course, this information is displayed only when the browser is configured to ask the user when a cookie arrives.

Figure 7.3. This cookie comes with an expiration date.





JavaScript Phrasebook(c) Essential Code and Commands
JavaScript Phrasebook
ISBN: 0672328801
EAN: 2147483647
Year: 2006
Pages: 178

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net