Java Reference
In-Depth Information
cookieStartsAt = cookieValue.indexOf(cookieName + “=”);
}
In the next if statement, you check to see whether the cookie has been found. If it hasn't, you set the
cookieValue variable to null .
if (cookieStartsAt == -1)
{
cookieValue = null;
}
If the cookie has been found, you get the value of the cookie you want from the document.cookie
string in an else statement. You do this by fi nding the start and the end of the value part of that cookie.
The start will be immediately after the equals sign following the name. So in the following line, you
fi nd the equals sign following the name of the cookie in the string by starting the indexOf() search for
an equals sign from the character at which the cookie name/value pair starts.
else
{
cookieStartsAt = cookieValue.indexOf(“=”, cookieStartsAt) + 1;
You then add one to this value to move past the equals sign.
The end of the cookie value will either be at the next semicolon or at the end of the string, whichever
comes fi rst. You do a search for a semicolon, starting from the cookieStartsAt index, in the next line.
var cookieEndsAt = cookieValue.indexOf(“;”, cookieStartsAt);
If the cookie you are after is the last one in the string, there will be no semicolon and the cookieEndsAt
variable will be -1 for no match. In this case you know the end of the cookie value must be the end of
the string, so you set the variable cookieEndsAt to the length of the string.
if (cookieEndsAt == -1)
{
cookieEndsAt = cookieValue.length;
}
You then get the cookie's value using the substring() method to cut the value that you want out of the
main string. Because you have encoded the string with the escape() function, you need to unescape it
to get the real value, hence the use of the unescape() function.
cookieValue = unescape(cookieValue.substring(cookieStartsAt,
cookieEndsAt));
}
Finally you return the value of the cookie to the calling function.
return cookieValue;
Search WWH ::




Custom Search