cookieName=cookieValue; expires= expirationDateGMT; path=URLpath; domain=siteDomain
Breaking this down, the first part of the string gives the cookie a name and assigns it a value. This is the only mandatory part of a cookie; the rest of the string is optional. Next is the expiration date of the cookie; when this date is reached, the browser automatically deletes the cookie. The expiration date is followed by a URL path, which lets you store a URL in the cookie. Finally, you can store a domain value in the cookie.
1. |
function nameFieldInit() {
First, set up the function nameFieldInit() to define the value of the cookie. This function is called when the window has completed loading.
|
2. |
Next, we initialize the variable userName with a null value.
|
| |
3. |
if (document.cookie != "") {
userName = document.cookie. split("=")[1];
We begin a conditional test by first checking that the object document.cookie does not contain a null value. The method split("=") splits a cookie into an array, where cookieField[0] is the cookie name and cookieField[1] is the cookie value. Note that cookieField can be any variable that you want to use to store a particular cookie's fields. So you assign userName the value returned by document.cookie.split("=")[1], that is, the cookie value.
|
4. |
document.getElementById("nameField") .value = userName;
Setting getElementById("nameField").value puts the user's name into the text field when the page loads if there's a name stored in the cookie file.
|
5. |
document.getElementById("nameField") .onblur = setCookie;
The onblur event handler (see Chapter 1) calls the setCookie() function when the user leaves the text field.
|
6. |
Now begin a new function, called setCookie().
|
7. |
var expireDate = new Date();
Get the current date, and put it into the new variable expireDate.
|
8. |
expireDate.setMonth(expireDate. getMonth()+6);
This line gets the month portion of expireDate, adds 6 to the month, and then sets the month portion of expireDate to the new value. In other words, it sets the expiration date of the cookie we're creating to six months in the future.
|
| |
9. |
var userName = document.getElementById("nameField").value;
This line creates a new userName variable and assigns it whatever the user typed into the text field. The userName variable has to be created twice (once inside each function) because it's not a global; that is, we're using it inside each function, but we're not expecting it to keep its value across functionsit's new each time.
|
10. |
document.cookie = "userName=" + userName + ";expires=" + expireDate.toGMTString();
Here's where we write the cookie. We're setting document.cookie (remember, a cookie is just a text string, so you can use the same text string techniques to build it, like using the + sign to combine things) to contain the user's name and the cookie expiration date. The toGMTString() method converts the expireDate Date object into a text string so that it can be written into the cookie.
|