Handling Multiple Cookies

You will often want to deal with more than one cookie at a time, and Script 10.8 shows you how to read from more than one cookie and display the information. This example shares a fair amount of code with the “Using Cookies as Counters” example.

To handle multiple cookies:

1.
var lastVisit = cookieVal("pageVisit");
We start off by looking for a cookie named pageVisit by passing that string to the cookieVal() function. It returns a value, which is then stored in lastVisit.
2.
if (lastVisit == 0) {
  lastVisit = "";
}
If the value of lastVisit is zero, then put a null value into lastVisit. We now know the user has never been here before.
Script 10.8. Use an array to deal with multiple cookies in a single script.
window.onload ...

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.