Document. Cookie. indexof

Source: Internet
Author: User
Tags cookie names

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 ));

}

}
 
}

 

Explanation:

1. When a cookie is stored in the system, it is saved in the form of "Cookie name 1 = cookie value; cookie name 2 = cookie value.

2. Document. Cookie is a string that returns all cookies.

3. Document. Cookie. Index (c_name + "=") is the location where the cookie name is obtained throughout the cookie. (Add "=": Take the cookie name and the location equal to the number. For example, the whole cookie is "user = Tony; passwd = user123". When you look for the cookie value named "user, is to find the location of "user ="; if it is not equal to the number, it will find the "user" in other locations, for example, there are also user characters in the password, however, not all cookie names in cookies are unique, so adding an equal sign will not cause errors. )

4. c_start = c_start + c_name.length + 1 is to add the cookie Name Length and 1 to the previously obtained location, that is, to confirm the location after the "=" symbol of the cookie name.

5. c_end = Document. Cookie. indexof (";", c_start) is found from the position in the previous step until the position.

6. Document. Cookie. substring (c_start, c_end) is the string that follows the equal sign of the cookie name and comes before the semicolon, that is, the cookie value.

 

================================

Better cookie reading

When writing code, we often use some open code, especially the frequently-used write functions such as reading and writing cookies. For example, I like to copy some source code, but it is not perfect. For example, it provides the following 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 (document. Cookie. substring (c_start, c_end ));

}

}

Return "";

}

The above code is used to read the cookie, but it has a problem. For example, if the cookie contains two fields "kuin = 12345" and "uin = 67890" at the same time, if

Kuin is located in front of uin in cookie, so the above Code cannot read the uin value, because it will mistakenly read kuin as 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 "";

}

Supplement:If there are a large number of values in the cookie, we do not need to constantly use getcookie () to read it. A more efficient way is to directly unbind all cookies during the first read.

The analysis result is stored in an object as follows:

Cookie = {

Uin: 12345,

Kuin: 59432,

NICK: floppy

};

(Conversion) document. Cookie. indexof Interpretation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.