the explanation of Document.cookie.indexofReproduced
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, the cookie is stored in the system is "cookie Name 1 = cookie value;" Cookie Name 2 = cookie value "In such a way as to save."
2, Document.cookie is to return the string containing all cookies.
3, Document.cookie.index (c_name + "=") is to get the cookie name in the entire cookie position. (Plus "=": Take the cookie name plus the position equal to the number, for example, the whole cookie is "user=tony; Passwd=user123 ", when looking for a cookie value named user, is to look for" user= "position, if not equal to the number, you will find the other location of" user ", such as the back of the password inside also have the user character, But the cookie name is not the only one in the cookie, so adding the equals number will not make an error. )
4, C_start = C_start + c_name.length + 1 is to use the previously obtained position plus the length of the cookie name plus 1, is to determine the position in the cookie name "=" after the symbol.
5. C_end = Document.cookie.indexof (";", C_start) is found from the position of the previous step until ";" 's 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.
==================== Better Read cookies
When writing code, we often use some open code, especially read and write cookies, which are very often written and uninteresting features. For example, I like to copy some of the source code to use, but it is not perfect, such as it provides the following paragraph:
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 (document.cookie.substring (c_start,c_end));
}
}
Return "";
}
The above code is used to read cookies, but it has a problem, such as when there are two fields "kuin=12345" and "uin=67890" in the cookie, if
Kuin is in front of the UIn in the cookie, the code above cannot read the value of UIn because it will mistakenly read Kuin as a uin.
So how to fix it, the following is the optimized code:
function GetCookie (c_name)
{
if (document.cookie.length>0)
{
var Tmp_cookie = Document.cookie,
TMP_C1 = (Tmp_cookie.indexof ("+c_name+ =") >0)? (Tmp_cookie.indexof ("" +c_name+ "=") +1): 0,
TMP_C2 = (Tmp_cookie.indexof (";" +c_name+ "=") >0)? (Tmp_cookie.indexof (";") +c_name+ "=") +1): 0,
TMP_C3 = (Tmp_cookie.indexof (c_name+ "=") ==0)? 0:-1,
C_START=TMP_C1 | | TMP_C2 | | TMP_C3;
if (c_start!=-1)
{
C_start=c_start + c_name.length+1;
var c_end=tmp_cookie.indexof (";", C_start);
if (c_end==-1) c_end=tmp_cookie.length;
Return (tmp_cookie.substring (c_start,c_end));
}
}
Return "";
}
Add: If there are a large number of values in the cookie, we do not need to constantly use GetCookie () to read, more efficient way is the first time to read the whole cookie directly solution
precipitates and is stored in an object, as follows:
Cookie = {
uin:12345,
kuin:59432,
Nick:flondon
};