Cookie parameters in JavaScript
The way to create a cookie in the user's computer using JavaScript is to assign a value to the 'document.cookie' property, which is of the string type. The format of this string is:
<name>=<value> [;expires=<datevalue>] [;domain=<domainname>]
[;path=<pathname>] [;secure]
I will detail now the characteristics of each of the parameters, as well as give some examples.
Name and value
They must be strings of characters that do not include the space, the semicolon, or the comma. If one or more of these three characters must be used, they must be encoded as in a URL (url-encoded). This can be done by passing all the string through the 'escape' function.
Expires
This is the date that marks the end of validity of the cookie. Once this date is reached, the browser should not give it out any more. The format of this date is "Wdy, DD-Mon-YY HH:MM:SS GMT". For example, "Thu, 11-Aug-05, 10:14:33 GMT". The JavaScript programming guide says, "Although it's slightly different from this format, the date string returned by the Date method toGMTString can be used to set cookie expiration dates."
A date can be specified in a variety of ways:
now = new Date(); // the current date and time
xmas = new Date("December 25, 2001 21:30:00");
xmas = new Date(2001,11,25);
xmas = new Date(2001,11,25,21,30,0)
The 'toGMTString' method converts the date to GMT (UTC) using the operating system's time-zone offset. If the time-zone offset is -3, the result of applying this method to the last example given would be:
Wed, 26 Dec 2001 00:30:00 UTC
The final format depends on the platform where JavaScript is running. If no time is given, zero is assumed. If the expiration-date parameter is not included, the cookie is deleted when the browser is closed. To delete a cookie whose validity has not expired, the same cookie must be sent by the server with a date before the current one.
Domain
The domain parameter is used to determine to which servers the browser should send the cookie. If no domain is specified, the cookie will be returned only to the server that set it. Let us assume that the cookie was set by a page in the domain "www.domain.com". In this case, it will only be sent with requests for pages in that domain. If the parameter was specified as ".domain.com", it will be sent to pages in "one.domain.com", "two.domain.com", etc. It is required that the domain parameter includes at least two periods.
Path
The path parameter specifies which pages within the domain can receive the cookie. When this parameter is not included, it is assumed as the path of the URL that created the cookie. For example, if the cookie was created by "www.domain.com/articles/mypage.html" the path will be "/articles". A path of "/" makes that all pages in the domain receive the cookie.
Secure
The presence of this keyword indicates that the cookie should only be sent using the secure HTTP protocol (HTTP+SSL), i.e., pages whose URL begins with "https://".
When the browser makes a request to a Web server, it searches its cookie list to determine if some cookie should be sent with the request. First, the domain is considered. Of all cookies matching the domain, those with a matching path are selected. If your cookie is created by a CGI script in a directory "/cgi-bin", and must be received by a page elsewhere in the domain, it is important to
specify a path of "/" during the creation.
Multiple cookies can be set and retrieved by a single page. When a cookie is sent with the same name as an existing one, the previous cookie is overwritten. Successive assignments to the 'document.cookie' property with different names will effectively create several cookies. When more than one cookie is to be sent by the browser, they are ordered depending on their path: those with the more specific path are sent first.
Calculating an expiration date
JavaScript store dates internally as the number of milliseconds since January 1, 1970, 00:00:00. This value is returned by the 'getTime' method and can be used to assign a date and time to another date object employing the 'setTime' method.
Such an object is created as follows:
aDateObject = new Date()
This creates a new object whose value is today's date and time. To display that value you can use:
document.write(aDateObject.getTime())
Expiration dates are not usually programmed as fixed dates. Instead, they are dynamically assigned dates calculated using the current date as a start point. Getting a date a month or a year later than today is easy using the 'getTime' and 'setTime' methods. We already saw how to get today's date; we only need to know how to obtain an interval of one month or one year to add to that value. All you have to remember is that the unit is the millisecond, and the following equivalencies.
1 second = 1,000 milliseconds
1 minute = 60 seconds
1 hour = 60 minutes
1 day = 24 hours
One month (30 days) can be calculated as 1000 * 60 * 60 * 24 * 30. An expiration date a month from today is obtained by these two statements.
var expirationDate = new Date();
expirationDate.setTime(expirationDate.getTime()
+ 1000*60*60*24*30);
Previous | Contents | Next
|