O'Reilly logo

JavaScript Application Cookbook by Jerry Bradenbaugh

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

cookies.js

Practical uses

Individual hit counters, form repopulation, user preferences settings

Version requirement

JavaScript 1.1

Functions

getCookieVal(), GetCookie(), DeleteCookie(), SetCookie()

You want client-state management? How about cool web site greetings for repeat visitors? Need to set up a language switching interface or other user preferences? This code makes setting and getting cookie information really easy. Figures 6-2, 6-3, and 6-4 show cookies.html in action. Notice in Figure 6.2 that the first time the page is loaded, the user is prompted to provide a name. Figure 6.3 displays the greeting that the first-time visitor receives. Figure 6.4 shows that repeat visitors are welcomed back warmly with a personalized hit count.

First timers fill in their name once . . .

Figure 6-2. First timers fill in their name once . . .

... get a newcomer’s welcome . . .

Figure 6-3. ... get a newcomer’s welcome . . .

... then become one of the gang

Figure 6-4. ... then become one of the gang

This is definitely a simple example of cookie power. Chapter 7 , applies this same code to “remember” user preferences. By the way, if you haven’t fully digested the concept of a cookie, check out the Unofficial Cookie FAQ at http://www.cookiecentral.com/unofficial_cookie_faq.htm. As unofficial as ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required