|
What Are Cookies? |
Enable and Disable Browser Cookies |
JavaScript Cookies
Here is a sample of your current cookies sent by this site:
Following "Cookie String" in the first
cell, you should see a print out of the document.cookie JavaScript
object. Document.cookie is a string containing all the cookies sent by whatever domain you are presently in,
separated by semicolons. To facilitate this example, we are filtering
the string so that you will see only cookies sent to
you by this page. The string is linked with a physical cookie
file on your hard drive, but it only displays the name/value data pair
and keeps all the other cookie attributes hidden. If this is your first visit
to this particular page, the document.cookie string print out should be
empty because we haven't sent you any cookies yet. The second cell
contains the sentence "Hello cookie1, your favorite color is
cookie2." Again, if this is your first time using this script
cookie1 and cookie2 will have values of null
because they don't yet exist. The form to the left allows you to set the
value of two cookies. The first stores a string intended for your name and
the second stores a string intended for your favorite color. You may also
delete all cookies sent to you by this page by clicking on the
clearCookies button.
Try entering your name and clicking
setCookies. Leave the favorite color text box blank. You'll notice in the
Cookie String that you've set the cookie named userName to
whatever you typed in as your name. You'll also notice that part of the
sentence is now filled in with your name. You've just witnessed the
reading and writing of a cookie. When you clicked on the setCookies button
you activated a JavaScript function that told your browser to send you a cookie
called username with a value of whatever you typed into
the text box. This page then reloaded and made
three access requests to the cookie on your hard drive. First it read
the entire document.cookie string, filtered out the cookies not sent by this page,
and printed it out as the Cookie String. The second and third requests parsed
document.cookie searching for the individual cookies, grabbed their values, and
printed them in that sentence.
Now enter a new name and a favorite color and click
setCookies. You'll notice that the new cookie favColor has
been appended to document.cookie. You'll also notice that the userName
cookie has a your new value. Now that you have these two cookies,
you can modify them all you like. You should never get more then two
cookies from this script. Once a cookie exists, a new definition of a cookie
with the same name will just modify the existing cookie.
We sent
you two persistent cookies, so they should be on your hard drive in some
form depending on your browser. If you're interested in seeing what the
physical cookie file looks like you'll have to locate it. If you're using
Netscape look for the file called 'cookies.txt' on the PC or 'magiccookie' on
the mac. In later versions of Netscape it should be in your personal user
sub-folder of the /Program Files/Netscape/. In Internet Explorer, cookies
for each domain are stored in separate files. In later version of IE the
cookies should be stored in the Temporary Internet Files folder with the rest of
the cache. The file should be named
'Cookie:yourUsername@webmaster.info.aol.com.txt.' Open the cookie
files and poke around. In the Netscape cookies.txt, you'll notice cookies from
the same domain are grouped together, but each cookie is given it's own
line. In IE, cookies from each domain are given their own file, but the
cookies are stored on the same line separated by delimiters.
Now click the clearCookies button. You'll
notice the Cookie String is now empty. If you check for the cookies on
your hard drive you'll see that they're gone. When you clicked on the
clearCookies button you activated a JavaScript function that set the expiration
dates of the cookies to a year ago. This negative dating has the effect of
immediately expiring and deleting the cookie. It's important to note that
each cookie had to be expired individually. When performing operations on
one, multiple, or all cookies, document.cookie must be parsed, each cookie must
be singled out, and the operations must be performed on each individual
cookie. In this way document.cookie acts as a string in someways, but is
more like an array in others.
If you're interested in seeing the specific
JavaScript functions responsible for the cookie manipulations on this page,
just view source and check out the code within the [script] tags.
Remember, this code can be used as a learning tool, but is property of
America Online, Inc. and will not be supported.
|