Java Reference
In-Depth Information
11
Storing Information:
Cookies
Our goal as web site programmers should be to make the web site experience as easy and pleasant
for the user as possible. Clearly, well-designed pages with easily navigable layouts are central to
this, but they're not the whole story. You can go one step further by learning about your users and
using information gained about them to personalize the web site.
For example, imagine a user, whose name you asked on the fi rst visit, returns to your web site. You
could welcome her back to the web site by greeting her by name. Another good example is given
by a web site, such as Amazon's, that incorporates the one-click purchasing system. By already
knowing the user's purchasing details, such as credit-card number and delivery address, you can
allow the user to go from viewing a book to buying it in just one click, making the likelihood of the
user purchasing it that much greater. Also, based on information, such as the previous purchases
and browsing patterns of the user, it's possible to make book suggestions.
Such personalization requires that information about users be stored somewhere in between their
visits to the web site. Previous chapters have mentioned that accessing the user's local fi le system
from a web application is pretty much off limits because of security restrictions included in brows-
ers. However, you, as a web site developer, can store small amounts of information in a special
place on the user's local disk, using what is called a cookie . There may be a logical reason why they
are named cookies, but it also provides authors with the opportunity to make a lot of second-rate,
food-related jokes!
Baking Your First Cookie
The key to cookies is the document object's cookie property. Using this property, you can create
and retrieve cookie data from within your JavaScript code.
You can set a cookie by setting document.cookie to a cookie string . You'll be looking in detail at
how this cookie string is made up later in the chapter, but let's fi rst create a simple example of a
cookie and see where the information is stored on the user's computer.
Search WWH ::




Custom Search