Using Cookies as Counters

Because cookies are persistent, that is, because they are available across multiple sessions between a Web server and browser, you can use cookies to store how many times a particular user has accessed a page. But this isn’t the same thing as the page counters you see on many Web pages. Because a cookie is specific to a user, you can only tell that user how many times he or she has visited; you can’t use cookies to tell all users how many times the page has been hit. Still, it’s useful to know how to create such an individual counter, and you can adapt Script 10.6 for other purposes, too (see Tips).

To use a cookie as a counter:

var expireDate = new Date();
These two ...

Get JavaScript and Ajax for the Web: Visual QuickStart Guide, Seventh Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.