JavaScript document.cookie.indexOf

Source: Internet
Author: User

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

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.