Previous Page
Next Page

Chapter 10. JavaScript and Cookies

In Web terms, a cookie is a unique nugget of information that a Web server gives to your browser when the two first meet and which they then share with each return visit. The remote server saves its part of the cookie and the information it contains about you; your browser does the same, as a plain text file stored on your computer's hard disk.

As a JavaScript author, you can do many useful things with cookies. If your site requires registration, you can set cookies to store your readers' user names and passwords on their hard drives, so they don't need to enter them every time they visit. You can keep track of which parts of your site the user has visited and count the number of visits from that user.

There are many common misconceptions about cookies, so it's important to note what you can't do with them: you can't get any real information about the user such as their email address; you can't use cookies to check out the contents of their hard disks; and cookies can't transmit computer viruses. A cookie is just a simple text file on the user's hard disk where you, the JavaScript programmer, can store some information.

A cookie always includes the address of the server that sent it. That's the primary idea behind cookie technology: identification. Think of it as Caller ID for the Web, with variations on the themeeach Web site using cookies gives your browser a personalized ID of some sort, so that it can recognize you on the next visit. When you return to the Web server that first passed you a particular cookie, the server can query your browser to see if you are one of its many cookie holders. If so, the server can then retrieve the information stored in the cookie the two of you originally exchanged. Keep in mind that cookies just identify the computer being used, not the individual using the computer.


Previous Page
Next Page