If you want your cookie to contain more than just one piece of information, you can make the value of the cookie as long as you want (up to the 4000 character limit). Say you're looking to store a person's name, age, and phone number. You do something like this:
var the_cookie = "username:thau/age:older than the hills/phone:411";
document.cookie="my_happy_cookie=" + escape(the_cookie);
I use a slash to
separate property names and a colon to distinguish
the property name from
the property value. The slash and colon are arbitrary choices they
could be anything, like say, this:
var the_cookie = "username=thau&age=older than the hills&phone=411";
document.cookie="my_happy_cookie=" + escape(the_cookie);
The delimiters you
choose are up to you. Just remember what you used so you can decode the
cookie later.
As with our simple example, the easy part is
setting the cookie. Things get a little sticky, however, when it comes to
pulling the information out of the cookie when you need it. I suggest using
associative arrays to store all the information. For example, let's say you saved
this to someone's hard
drive:
my_happy_cookie=username:thau/age:older than the hills/phone:411
You could put the
information into a handy associative array like
this:
function readTheCookie(the_info)
{
// load the cookie into a variable and unescape it
var the_cookie = document.cookie;
var the_cookie = unescape(the_cookie);
// separate the values from the cookie name
var broken_cookie = the_cookie.split("=");
var the_values = broken_cookie[1];
// break each name:value pair into an array
var separated_values = the_values.split("/");
// loop through the list of name:values and load
// up the associate array
var property_value = "";
for (var loop = 0; loop < separated_values.length; loop++)
{
property_value = separated_values[loop];
var broken_info = property_value.split(":");
var the_property = broken_info[0];
var the_value = broken_info[1];
the_info[the_property] = the_value;
}
}
If you had this function in your
JavaScript, you could call it
like this:
var cookie_information = new Array();
readTheCookie(cookie_information);
And then you'd have cookie_information["username"], cookie_information["age"], and cookie_information["phone"] set correctly.
This may look a little funky, but actually it's not that hard. Here's a step-by-step analysis of what's going on:
- var the_cookie =
document.cookie;
- This is the easy part. It puts the cookie into a
variable.
- var the_cookie =
unescape(the_cookie);
- Undo the work of
escape().
- var broken_cookie = the_cookie.split("=");
var the_values = broken_cookie[1];
- These lines make
the_values equal to username:thau/age:older than the hills/phone:411.
- var separated_values = the_values.split("/");
- This makes an array called
separated_values that contains three elements:
separated_values[0] = "username:thau"
separated_values[1] = "age:older than the hills"
separated_values[2] = "phone:411"
- for (loop = 0; loop < separated_values.length; loop++)
- This loops
through the three elements of
separated_values.
- property_value = separated_values[loop];
- This grabs the current name:value
pair, the first one being username:thau.
- var
broken_info = property_value.split(":");
- This breaks the pair
into two elements into an array called broken_info
where
broken_info[0] = "username"
broken_info[1] =
"thau"
- var the_property =
broken_info[0];
- The first time through the loop,
the_property will be "username"
- var the_value =
broken_info[1];
- the_value will be
"thau."
- the_info[the_property] = the_value;
- Here's
where associative arrays come in handy. This makes
the_info["username"] = "thau".
So now, when you want the username
from the cookie, you can just say something like var the_name =
the_info["username"];.
- And so on
- Each time through the
loop, a new element gets added to the_info. At the end of the
loop is the_info["username"] = "thau", the_info["age"] = "old as the
hills" and the_info["phone"] = 411.
It
is a bit cumbersome, but it's the way I prefer to get large
quantities of information in and out of cookies. There are lots of ways to
do it, and if you find another way that you like, all the power to
you.
Okay, the last thing you need to know is what to do when
you're dishing out multiple cookies.
next page»