Ken Ward's Java Script Tutorial ...

Cookies 1

Setting and viewing cookies

Acknowledgement

Much of the work on cookies has been done by Bill Dortch, hIdaho Design bdortch@hidaho.com and this section is highly influenced by his work.

You can make a cookie by entering a name in the text box and pressing the 'Set Name' button. If you then press the 'Show cookie' button, you will see the contents of your cookie.

When you press set name, it calls a function called setcookie. This function uses the following code:

function setcookie(){

document.cookie = 'Cookie Name='+document.form1.cookieName.value;

}

We make the cookie (which is a string) contain the information 'Cookie Name=' plus the name we enter in the text box.

function showCookie(){

alert(document.cookie);

}

when we click the button, Show cookie, we get the contents of the cookie in the alert. I entered 'Ken' in the box, so the alert says: 'Cookie Name=Ken'.

So we have a cookie with some information. However, when we close the browser, and restart it our cookie has gone! Not much so far, but a start. The next step with cookies is to make them last a little longer.

Next: The next step

 

Ken Ward's HTML Guide...