• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

1. Strings > 1.9. Reading and Writing Strings for Cookies

Reading and Writing Strings for Cookies

NN 2, IE 3

Problem

You want to use cookies to preserve string data from one page visit to the next.

Solution

Use the cookies.js library shown in the Discussion as a utility for saving and retrieving cookies. To set a cookie via the library, invoke the setCookie( ) function, passing, at a minimum, the cookie’s name and string value as arguments:

setCookie ("userID", document.entryForm.username.value);

To retrieve a cookie’s value, invoke the library’s getCookie( ) function, as in:

var user = getCookie("userID");

Discussion

Example 1-1 shows the code for the entire cookies.js library.

Example 1-1. cookies.js library

PREVIEW

                                                                          

Not a subscriber?

Start A Free Trial


  
  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint