Example
It is time to join the script and the web page code. Insert the script in the HEAD tag. You do not need to type any line, the example is available to download.
This example saves the value of a form field in a cookie called DEVpapers.com and each time the user loads the document, a pop up alert appears with the value of the cookie. It will happen until the expiration date.
The function added is
|
function devpapersRead() { var visited=devpapersOpenCookie("DEVpapers.com");
if (visited!=null) alert(visited); } |
Then, we insert the following HTML code to invoke the JavaScript functions. As we said, every time the document is loaded (handled with the onLoad event), the function devpapersRead is called, and it alerts the user with the value of the cookie, if set. And each time users submit the form, the cookie is updated with new values.
<body bgcolor="#FFFFFF" onLoad="devpapersRead()"> <form action="" name="cookieForm" onSubmit="devpapersSave(document.cookieForm.cookieField.value)"> <input type="text" name="cookieField"> <input type="submit" name="cookieButton" value="Cookie"> </form> |
Conclusion
Using JavaScript, we can handle useful information to, for example, personalise web sites. We made this example trivial for educational purposes –even we alert the value of the cookie-, but we can handle more useful data.