Code:
function GetCookie (c_name)
{
if (Document.cookie.length > 0)
{
C_start = Document.cookie.indexof (c_name + "=");
if (C_start! =-1)
{
C_start = C_start + c_name.length + 1;
C_end = Document.cookie.indexof (";", C_start);
if (c_end = =-1)
{
C_end = Document.cookie.length;
}
Return Usescape (document.cookie.substring (C_start, c_end));
}
}
}
Explain:
1. When the cookie is stored in the system, it is "cookie name 1 = cookie value; Cookie Name 2 =cookie value "in such a way to save.
2. Document.cookie is a string that returns all the cookies that are contained.
3, Document.cookie.indexOf (c_name + "=") is the place where the cookie name is obtained throughout the cookie. (plus "=" Number: The location of the cookie name plus the equals sign, for example, the whole cookie is "user=tony;passwd=user123", when looking for a cookie value named User, is the location of the "user=", if not equal to the number, You will find the "user" in other locations, such as the user character in the following password, but not the cookie name is unique, so adding the equals sign will not be an error. )
4, C_start = C_start + c_name.length +1 is the length of the previously obtained position plus the cookie name plus 1, is to determine the location of the cookie name "=" after the symbol.
5, C_end = Document.cookie.indexof (";", c_start) is searched from the position of the previous step until ";" The location.
6, Document.cookie.substring (c_start,c_end) is the name of the cookie after the equal sign to the semicolon before the string, that is, the value of the corresponding cookie.
JavaScript document.cookie.indexOf