• Aucun résultat trouvé

Personalizing an Experience Using a Cookie

As you’ve probably experienced for yourself, cookies are used a lot by developers to personalize your experience while visiting a web site. For example, a cookie might be used to store your name and data about what types of information you’ve viewed on the site in the past. The next time you visit the site, a JavaScript reads the cookie and displays a web page that contains features that might be of interest to you. Developers would know your preferences by monitoring your selections from previous visits.

A common use of cookies by e-commerce web sites is to point out merchandise that was added to the site since the user’s last visit. This is accomplished by storing the date of the last user visit in a cookie. On subsequent visits, the cookie is read and the date compared to the current date. The JavaScript then notifi es the user whether any new merchandise of interest has been received since his or her last visit.

The next example shows how this is done. For the sake of this example, we as-sume that a cookie is already created and the value of the cookie is a date in the yyyy,mm,dd format. We’re also using a button on a form to trigger the JavaScript function. In a real application, the JavaScript function would be called from the onload event so that the web page could be personalized before being shown to the user.

The UpdateNotice() function is called when the button is clicked. This function determines whether a cookie exists by comparing the value of the cookie object to "", which is nothing (NULL). Notice that in the code we used the not operator to say, “the value of the cookie is not equivalent to NULL.” In other words, there is a cookie.

If the cookie exists, we then declare a new date variable and declare the CookiePrevVisit variable, initializing it with the value of the cookie. You saw how this is done previously in this chapter.

The value of the cookie is the date in the yyyy,mm,dd format. Remember that the value of a cookie is a string. We must convert the string to a date in order to compare dates. You convert a string that is a date format to a date by passing it to the con-structor of the Date object. This returns a date:

ch08.indd 174

ch08.indd 174 4/26/2005 10:15:35 AM4/26/2005 10:15:35 AM

var PreviousVisit = new Date(CookiePrevVisit)

The getTime() method of the Date variables is then called to return the time value of the dates. These are then compared. If Today is greater than the PreviousVisit, we know that the user has returned and we display an alert dialog box that contains a welcome back message. If this was a real application, we would perform addi-tional comparisons to determine whether we received any new merchandise that might be of interest to the user since his or her last visit; if so, we’d create a web page that highlights those items. The split() function assumes there’s only one cookie.

<!DOCTYPE html PUBLIC

"-//W3C//DTD XHTML 1.0 Transitional//EN">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<title>New Features</title>

<script language="Javascript" type="text/javascript">

<form name="CookieWriter" action="" >

ch08.indd 175

ch08.indd 175 4/26/2005 10:15:36 AM4/26/2005 10:15:36 AM

<INPUT name="Reset" value="New Visit"

type="button" onclick=" UpdateNotice()"/>

</FORM>

</body>

</html>

Looking Ahead

Cookies provide a convenient way to keep track of visitors to your web site and to personalize their experience by storing and retrieving small amounts of information on the visitor’s computer. Cookies don’t do any harm to a user’s computer, because a cookie is simply plain text and cannot contain viruses or other kinds of destructive programs.

Depending on the needs of your application, your cookies can remain on your visitor’s computer until the browser session is completed or until the expiration date of the cookie is reached. You set the expiration date. If you don’t set an expiration date in JavaScript, the cookie is automatically deleted when your visitor exits the browser.

Information is stored as a name-value pair. You provide a name for the informa-tion and the value is the informainforma-tion. Although you can create multiple cookies, the browser is required to accept only 20 from each web server.

Your cookies can be accessed only by applications from your web server. Ap-plications from other web servers cannot access your cookie. Likewise, you cannot access a cookie created by an application from another server.

With cookies under your belt, it is time to move on to another cool feature—

controlling browser windows from within a JavaScript.

Quiz

1. True or False. You cannot delete a cookie.

a. True b. False

2. A cookie takes the format of a a. Pair-name value

b. Pair-value name

ch08.indd 176

ch08.indd 176 4/26/2005 10:15:36 AM4/26/2005 10:15:36 AM

c. Value-name pair d. Name-value pair

3. The best time to read a cookie is a. onblur

b. onload c. onselect d. onchange

4. The expiration date is stored in a cookie as a. A GMT string

b. A Date data type c. A digital sequence type d. A sequential numeric type 5. The best time to create a cookie is

a. onblur b. onload

c. Any time it make sense to do so while a visitor is visiting your web site d. onchange

6. A cookie is a. A variable b. A Date variable c. A text variable d. An object

7. True or False. You can use a cookie to explore a visitor’s hard disk.

a. True b. False

8. True or False. Your JavaScript actually writes a cookie to a visitor’s hard disk if you set an expiration date for the cookie.

a. True b. False

9. True or False. The address of your web server is included in a cookie.

a. True b. False

ch08.indd 177

ch08.indd 177 4/26/2005 10:15:36 AM4/26/2005 10:15:36 AM