JavaScript and Cookies


A cookie is a unique item of information that a web server gives to your browser when the two first make contact. That cookie is then shared with each subsequent meeting.

General Uses for Cookies

The Truth About Cookies

It is widely thought that cookies can be used to retrieve user information, email addresses and PC hard-drive contents. This is completely untrue. Cookies can not transmit computer viruses. A cookie is a simple text file placed onto a user's hard-drive where information is stored.

A cookie always includes the address of the server that sent the cookie. Cookie technology original idea was for that of identification.

Where Cookies are Stored ?

Cookies are stored in a cookie-jar. This cookie-jar is located in various places on your hard-drive depending on your browser. The Netscape cookie-jar is in C:\Program Files\Netscape\Users\<username>\cookies.txt. For the Microsoft Internet Explorer the cookie-jar is placed in C:\Windows\Profiles\<username>\cookies or C:\Windows\cookies.

Writing A Simple Cookie

Note that the only mandatory field entries required for a cookie are the name and value pair.

We will now attempt to retrieve the cookie which we have just written in the above example.

Reading A Simple Cookie

Using Cookies as Counters

Cookies are persistent. Thus cookies are available across multiple sessions between a web server and a browser. Therefore cookies can be used to store information that will be accessed at a later date.

A Cookie Counter

Deleting Cookies

The easiest way to delete cookies is to set the expiration date to allow automatic deletion.

Deleting Cookies

Multiple cookies on a single page would look as shown below.

"pageHit=<Number of hits on page>;
pageVisit=<Date of page visit and cookie creation>;
userName=<The User Name in the text box>;
expires=<Date of cookie expiration and thus deletion>;"
"pageHit=<Number of hits on page>;
pageVisit=<Date of page visit and cookie creation>;
userName=<The User Name in the text box>;
expires=<Date of cookie expiration and thus deletion>;"