Retrieving cookies

Unfortunately, setting cookies is the easy part. Retrieving the values from the cookie string requires a little more code. Because all of the cookies for a single document are stored in the same string, it's necessary to parse the string for the required information.

The way I approach the problem is to set up a loop to cycle through the string, searching for the name value using the substring() method. The substring() method is a method of the string object and accepts two arguments. The first argument specifies the beginning of the substring, and the second argument specifies the end of the substring. For example, the following code would return the string 'lo W':

"Hello World!".substring(3, 6);

A popular method of searching for the name value is to set up an index variable, and compare the name value with the substring starting the index variable and ending with the character that is the same length from the index variable as there are characters in the name value. In code, this would look like:

if(name==document.cookie.substring(index, index + name.length);

The index variable would be incremented by a loop, which would cycle through the entire string. Eventually, if the name value is anywhere in the cookie string, it should be found.

However, this method is somewhat inefficient, and possibly error-prone. The method that I use to search for a cookie's name relies on the indexOf() method to search after each semicolon in the string. The indexOf() method is another method of the string object, and takes two arguments -- the string to search for, and the index of the string from which to begin the search.

Conceptually, the code will check for the name value at the beginning of the cookie string. If it is not found, the code jumps to the next semicolon, skips the following space and then checks the next string. If the name value is not found, the function returns null. However, if the name value is found, the code jumps past the name string, skips the following equals sign, and then extracts the value property.

Extracting the value from a cookie requires checking for a semicolon marking the beginning of the next cookie, and then returning the substring from the start of the value to the semicolon. However, if the cookie is the last cookie of the document, there will be no trailing semicolon, in which case we will have to return the string from the beginning of the value and ending at the last character of the string. The box below shows how this would look in code.

function getCookie(name)
{
cookieString = document.cookie;

for(i=0; I{{cookieString.length;)
	{
	if(cookieString.substring(i, i + name.length) == name)
	{
	i += (name.length + 1);
	if(cookieString.indexOf(";", i) {{ i)
		return unescape(cookieString.substring(i, cookieString.length));
	else
		return unescape(cookieString.substring(i, cookieString.indexOf(";", i)));
	}
i = cookieString.indexOf(";", i);
if(I{{0)
	return null;
else
	i += 2;
}

// This line of code should never execute!
return null;
}