JavaScript : Smart Sites and Cute Cookies
Contents ]
Dr Benton

Step-by-Step Guide to Baking Cookies

As you might well have guessed, we need a function which enables us to write data in the cookie stored on your visitor's hard disk. As for the actual saving process, this will be one for the visitor's browser. But it is up to you to define a saving pattern so that saved data can be retrieved each time your visitors return to the site. The JavaScript code will have to execute the following steps:

  1. Check if there is a cookie on the visitor's computer.

  2. Read and update the cookie's data:

  3. - If a cookie already exists, the data are read and updated.

    - If a cookie does not exist, it is created and the initial data are written into it.

  4. Utilise the data in the cookie. In our example, this involves displaying a message to inform the user of the number of visits.

To carry out all these steps, we will use the browser's document.cookie object. This acts as a gateway for creating, writing and reading cookies. We will now create all the functions we need, one after the other.



  1   2   3   4   5   6   7