WebWoman Search

Cookies
aka, Persistent Client-Side HTTP Objects

Definition

A mechanism by which both the server and client, through JavaScript, store and retrieve information from the client side of the connection. The information is stored on the client computer for later access.

Uses

Limits

Ingredients

NAME=VALUE
Specifies the name of the cookie entry and the value associated with that named cookie. Sort of like setting a variable: variableName=VALUE. This is the only required component. No ; or , or whitespace is allowed.
Example: visitCount=5
expires=DATE (where DATE is of the form: Wdy, DD-MON-YYYY HH:MM:SS GMT)
Defines the lifespan of the cookie. Must be supplied in GMT format. If not specified, the cookie only exists until the browser is shut down.
Example: Mon, 10-Nov-1998 23:14:25 GMT
domain=DOMAIN_NAME
Identifies the valid cookie domain. By default, the domain of the server which generates the cookie response. Must have at least 2 or 3 periods.
path=PATH
Commonly "/". The path name of URL(s) that are allowed access to the cookie. That is, the cookie will be good for all subdirectories below the specified directory. By default, this is the path of the document associated with the cookie.
secure
Indicates whether you need a secure HTTP connection to access the cookie. If set to secure, the cookie will transmit only if the connection between the server and the browser is a secure one. Default: Not secure.

Syntax

document.cookie = "cookieName=cookieData[; expires=timeInGMTstring]
                                        [; path=pathName]
                                        [; domain=domainName]
                                        [; secure]"

The format and order are important. Optional parameters are listed in brackets.

Setting a cookie's expiration date

To calculate an expiration date, one year from today's date:

     var exp = new Date()
     var oneYearFromNow = exp.getTime() + (365 * 24 * 60 * 60 * 1000)
     exp.setTime(oneYearFromNow)

     document.cookie = "cookieName=cookieValue; expires=" + exp.toGMTString()

For one month from today's date replace oneYearFromNow with oneMonthFromNow:

     var oneMonthFromNow = exp.getTime() + (30 * 24 * 60 * 60 * 1000)
For one week from now:
     var oneWeekFromNow = exp.getTime() + (7 * 24 * 60 * 60 * 1000)

Retrieving Cookie Data

Retrieving cookie data with JavaScript is a pain because the entire cookie, including it's name-value pair is all contained in one string. Two or more cookies with the same domain and path will be stored together:

     username=Sam; pwd=devan728; visits=5
To use the data stored in the cookie, you first have to parse it out. That is why I recommend you use Bill Dortch's Cookie Functions listed below.

More notes on Cookies

Cookies cannot:

Examples:

References:

Bill Dortch's Cookie Functions
Why reinvent the wheel? These cookie functions were devised by Bill Dortch, an experienced JavaScripter and web site designer. You can use them to handle all of your cookie-related tasks in your web pages. I recommend placing them in an external JavaScript file for easy reuse. If the above link doesn't work, I've placed a copy here.
What Cookies Can Do For You
by Len Vishnevsky. Webmonkey 15 Jan 1998
So You Want a Cookie, Huh?
by Joe Burns, Ph. D.
WebWoman - Your web design & development training superhero!
WebWoman™ - Your web development superhero!      Phone: (843) 821-1466