|
Using Cookies
|
I. Introduction
Since values for variables only last as long as the particular
web page is displayed, it would be useful to have a method to
store small amounts of information on the users computer that can
be accessed by a web page as necessary. Cookies are the
answer! Browsers do not have to retain more than 300 cookies at
any one time nor more than 20 cookies per any one server nor more
than 4 KB of information per cookie. So cookies are not the
solution for storing large amounts of data. In our interest in
JavaScript, cookies are useful to store information about a
user's preferences for how our web pages are displayed or as a
temporary storage for values while a web page is re-rendered by
the browser.
II. Things to Remember
- A cookie will have a name and a stored value (a string of
information) plus it may have four optional attributes.
The first attribute is expires which gives the
maximum length of time the cookie is to survive on the
user's computer if not deleted by some other means. The
second attribute is path. A cookie is useful only
for the web page in which it is created or any web page
in the same directory. A path can be specified which will
overwrite this limitation, e.g., using a "/"
allows any web page on the same server to use that
cookie. The domain attribute can be used to allow
any server at a particular domain to use that cookie. For
example, a domain of "bridgewater.edu" would
let all servers at Bridgewater College to use a
particular cookie. The secure attribute when set
tells the server to exchange cookie information only
through a secure encryption scheme. For client-side
JavaScript, this attribute does not need to be set.
- In order not to run up against the limit of 20 cookies
per server on a user's machine, the preferred protocol is
not to use more than one cookie per web page. Instead,
several items of information can be appended into one
string using a colon, semi-colon or comma to separate the
individual items of info.
- The value for a particular cookie may not contain commas,
semi-colons or spaces. However, these can be stored
easily in by encrypting the value for the cookie using
the escape() function and then using the unescape()
function to decipher the value of a stored cookie before
it is used.
III. An Example and an Assignment
All of the simulations on my "Interactive Science
Activities on the Web" site at:
http://people.bridgewater.edu/~rbowman/ISAW/
use cookies to pass information from a previous page to the next
page. Go to that site and examine the "Planetary Orbit
Simulation." It passes five numbers to the next page which
does the actual plotting of the planetary orbit.
When you are ready for a challenge and some fun, take this
simulation and modify it for your purposes. Begin by stripping
out a lot of the non-critical code, e.g., the Site Meter code at
the bottom. Hope you enjoy it and do not get too many headaches!
Return to Ed Tech by
Bowman Home
Created and maintained by: Richard L. Bowman (
richard.bowman@edtechbybowman.net ); last updated: 31-Mar-11.