HTML and CSS Reference
In-Depth Information
Here, two additional fields have been added to the cookie:
name
and
custid
. The
values for these fields are taken respectively from the
Name
field and the
CustId
field in
the
Orders
form.
Reading a Cookie
One of the challenges of working with cookies in JavaScript is reading the cookie infor-
mation. To do this you need to extract the appropriate information from the cookie's text
string and place that information in the appropriate JavaScript variables. You can use
several of JavaScript's string functions to help with this task. To start, create a function
named readCookie(
fname
), where
fname
is the name of the field whose value you want
to retrieve. The initial code looks like
functionƒreadCookie(fname)ƒ{
ƒƒƒƒvarƒcookies=document.cookie;
}
where the text string of the cookie is stored in the
cookies
variable. In the text string,
each field name is followed by an equal sign, so you can use the
indexOf( )
method
(see Appendix F) to locate the occurrence of the text string
fname=
, where
fname
is the
field name you want to retrieve. You'll store this location in a variable named
startname
.
The command is:
startname=cookies.indexOf(fname+”=”);
For example, if
fname
=
custid
in the text string below,
startname
would have a value of
33, because
custid
starts with the thirty-third character in the text string.
cookie1=OrderForm;ƒname=Brooks;ƒcustid=20010;ƒtype=clothes
What if the field name is not found in the cookie? In this case,
startname
has a value of
-1, and you can create an
if…else
conditional statement to handle this contingency. To
simplify things for this example, you'll assume that this is not a concern, and continue.
Next you need to locate the field's value. This value is placed after the equal sign and
continues until you reach a semicolon indicating the end of the field's value, or until you
reach the end of the text string. The field's value then starts one space after the first equal
sign after the field's name. You'll locate the beginning of the field value, using the same
indexOf( )
method, and store that location in the
startname
variable. The command is:
startvalue=cookies.indexOf(“=”,ƒstartname)+1;
Here, you locate the text string =, starting at the point
startname
in the
cookies
text string.
You add one to whatever value is returned by the
indexOf()
method. In the text string
cookie1=OrderForm;ƒname=Brooks;ƒcustid=20010;ƒtype=clothes
the value of the
startname
variable is 40, because the “2” in “20010” is the fortieth char-
acter in the string.
Next you locate the end of the field's value, which is the first semicolon after the
startvalue
character. If the field is the last value in the text string, there is no semicolon
at the end, so the
indexOf( )
method returns a value of -1. If that occurs, you'll use
the length of the text string to locate the value's end. Once again, using the
indexOf( )
method, you'll store this value in the
endvalue
variable. The JavaScript command is:
endvalue=cookies.indexOf(“;”,startvalue);
if(endvalue==-1)ƒ{
ƒƒƒendvalue=cookies.length;
}
In the text string below, the value of the
endvalue
variable for the
custid
field is 45.
cookie1=OrderForm;ƒname=Brooks;ƒcustid=20010;ƒtype=clothes