Chapter 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 first 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 file system from a web application is pretty much off limits because of security restrictions included in browsers. 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 ...

Get Beginning JavaScript®, Fourth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.