[main] [misc] [graphics] [page design] [site design] [xhtml] [css] [xml] [xsl] [schema] [javascript] [php] [mysql]

HVCC Home
Blackboard HVCC
Blackboard Manual
Faculty Association

javascript main
1. javascript basics
2. core javascript
3. js statements
4. js functions
5. js arrays
6. js objects
7. debugging js
8. js client side
9. the js bom
10. js frames and windows
11. js forms
12. js regexp
13. js cookies
a. viewing cookies
b. writing js cookies
* c. reading js cookies
14. basic dhtml


print version

Note that all external links will open up in a separate window.

This is a stripped down version of these pages for older browsers. These pages are really meant to be viewed in a standards compliant browser.

Directions for surfing with audio.

Reading Cookies

These tutorials are about JavaScript and its use for client-side Web programming.

Reading Cookies

When you read a cookie, which is to say, when you look at the contents of the document.cookie object, what you get is a string that contains all the cookies you have permission to view. The string is a semi-colon separated list of name=value pairs. In order to get the information you want you need to extract it and them most probably unescape it to get at the information in the cookie.

There are numerous way to search a cookie string for information. You can use the String.indexOf( ); and String.substring( ); methods to extract the portion you are looking for. You can also use the String.split( ); method to convert the string into an array, and then you can just look for a value before the equals sign in each string that matches the cookie name you are looking for.

Here is an example of splitting the cookie into an array:

// create a temp array
tempArray = someStr.split(';');

// create an array of arrays
finalArray = new Array();
xyz = 0;
for (elName in tempArray) {
  finalArray[xyz] = tempArray[elName].split('=');
  xyz++;
  }

// test for our desired value
for (elName in finalArray) {
  if (finalArray[elName][0] == desired cookie) {
    process cookie value finalArray[elName][1];
    }

The advantage of doing it this two-step way is that some browsers include the cookie attributes as part of the returned cookies string, after the name value pair. This can mess up your cookie processing if you assume that there is just one cookie and therefore you only need to check for an equals sign. If you do this, then what you thought was your cookie value may actually be a string suffixed by the name of the next attribute, like "100;expires".

There is of course, also the possibility that while you think there is only one cookie set that you can access, there may be multiple cookies. This would produce the same type of error, since they are strung together in a semi-colon delimited list.

Don't unescape a string until after you have split it out. Otherwise special characters in the string may mess up the split.

When reading cookies, you only have access to the name=value pairs. You do not have access to any of the other cookie properties that may be set for that cookie.

[top]