Next page | Contents page |

Cookies

document.cookie is a semicolon-separated string of name=value pairs for the HTML page.

This string is kept by the browser. It is typically used to to store settings that the user has made for the page so that they can be reapplied automatically the next time the page is loaded. However, HTML5 has introduced a new way of doing this called local storage. We will look at local storage in a subsequent page.

In a script the string can have a new name=value pair added to it by writing something like
document.cookie = "myCookie=" + value;
in which "myCookie" is a key for finding the relevant value in the string again. However this lasts only for the current browser session and for the current page and pages loaded from the same directory. To make it persist or to do certain other things there are some optional strings that can be appended when setting the cookie:

Read the whole string as var s = document.cookie and then find the relevant key by using s.indexOf ("myCookie=") or s.split (";") to get the cookie value. You cannot read the optional attributes.

The following points should also be noted.

For more information about the use of cookies see Mozilla's reference page.

Next page | Contents page |