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:

1.
var expireDate = new Date();
expireDate.setMonth(expireDate.getMonth()+6);
These two ...

Get JavaScript and Ajax for the Web: Visual QuickStart Guide, Seventh 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.