| Home |
JavaScript and Cookies |
|||||
| JavaScript |
|
Cookies have been the subject of a lot of controversy right from the time Netscape incorporated it into it's browser. A cookie is a small piece of information that a web page can store on your machine and retrieve it on your subsequent visits. To protect your privacy, a cookie set by one particular site cannot be accessed by another site. So if you had any plans to surprise your visitors with messages like "I know what you did yesterday blah blah blah...", you can forget it right now. To learn how to use cookies in Javascript, I went to Netscape's
Javascript Guide and found these two functions that I thought were
very useful. I have modified it to make it look simpler but the functionality
is essentially the same. To use these functions in your page, add the above code between the A cookie basically consists of three pieces of information: a variable
name, an associated value and an expiration date. For example, when a
person visits your web page, you can have a form asking his/her name and
save it as a cookie so that you can greet your visitors by name on their
subsequent visits. In this case, you would create a cookie called 'visitor'
and assign it the value the user enters in the form and set the expiration
date to a few months ahead of the current day. You access cookies by using
the
The above line will create a cookie called The setCookie("visitor", "yoda", expireDate); Of course you don't want to set the name of every visitor as 'yoda'
(unless you are a big Star Wars
fan). Let us create a form to ask the vistor for his name and use that
to set the cookie. Here is the result. Please enter your name and click the 'Save' button to save your name as a cookie.
This example is getting pretty long and the complex part is just beginning.
Reading a cookie back is not as simple as writing a cookie because if
you ask for the value of You will see the cookie you set earlier, but you will also see other
cookies that this page automatically sets. (These cookies are used by
the banners on this page to make sure that the same banner does not get
diplayed over and over again). Extracting the value of a particular cookie
out of this mess can be tiresome but fortunately we already have the See Also: |
||||
| Today's Date | ||||||
| Dropdown Menu | ||||||
| Mouse Over | ||||||
| Status Bar | ||||||
| Status Runner | ||||||
| Background Color | ||||||
| Script Tester | ||||||
| Mini-Windows | ||||||
| Cookies | ||||||
| Expiration | ||||||
| Java Applets | ||||||
| Available CGI | ||||||
| META Tags | ||||||
| Server Elements | ||||||
|
|
||||||
|
Binary IT Solutions, Inc. |
||||||