Summary of js cookie reading methods, and js cookie reading

Source: Internet
Author: User

Summary of js cookie reading methods, and js cookie reading

This example summarizes the methods for reading cookies from JavaScript. Share it with you for your reference. The specific implementation method is as follows:

There are many methods to read cookies from js. The following example functions use the split function to split and document. cookie obtains all cookies and then uses for to traverse all arrays. If the cookie name is the same, this cookie is what we are looking.

Method 1
Copy codeThe Code is as follows: var acookie = document. cookie. split (";");
Function getck (sname)
{// Obtain a single cookie
For (var I = 0; I <acookie. length; I ++ ){
Var arr = acookie [I]. split ("= ");
If (sname = arr [0]) {
If (arr. length> 1)
Return unescape (arr [1]);
Else
Return "";}}
Return "";
}

Method 2
Copy codeCode: function getcookie (objname) {// obtain the cookie value of the specified name
Var arrstr = document. cookie. split (";");
For (var I = 0; I <arrstr. length; I ++ ){
Var temp = arrstr [I]. split ("= ");
If (temp [0] = objname) return unescape (temp [1]);
}
}
Method 3
Copy codeThe Code is as follows: function getcookie (cookiename ){
Var cookiestring = document. cookie;
Var start = cookiestring. indexof (cookiename + '= ');
If (start =-1) // cannot be found
Return null;
Start + = cookiename. length + 1;
Var end = cookiestring. indexof (";", start );
If (end =-1) return unescape (cookiestring. substring (start ));
Return unescape (cookiestring. substring (start, end ));
}

Method 4
Copy codeThe Code is as follows: function readcookie (name)
{
Var cookievalue = "";
Var search = name + "= ";
If (document. cookie. length> 0)
{
Offset = document. cookie. indexof (search );
If (offset! =-1)
{
Offset + = search. length;
End = document. cookie. indexof (";", offset );
If (end =-1) end = document. cookie. length;
Cookievalue = unescape (document. cookie. substring (offset, end ))
}
}
Return cookievalue;
}

I hope this article will help you design javascript programs.


How does js read cookies?

Function SetCookie (sName, sValue)
{
Document. cookie = sName + "=" + escape (sValue );
} Function GetCookie (sName)
{
// Cookies are separated by semicolons
Var aCookie = document. cookie. split (";");
// Alert (document. cookie );
For (var I = 0; I <aCookie. length; I ++)
{
// A name/value pair (a crumb) is separated by an equal sign
Var aCrumb = aCookie [I]. split ("= ");
If (sName = aCrumb [0])
Return unescape (aCrumb [1]);
} // A cookie with the requested name does not exist
Return "";
}

For JavaScript to read cookies

Window. onload = function GetCookie (){
Var CookieStr = document. cookie;
// Obtain the cookie you wrote. [cookie content: CookieInfo = Name = GTweb & Version = 2.0]
Var GetName = CookieStr. indexOf ("Name") + 5; // obtain the location of Name = in the cookie
Var mark = CookieStr. indexOf ("&"); // obtain the & position of the symbol in the cookie
If (CookieStr. substring (GetName, mark )! = "GTweb "){
// Determine whether the string between "Name =" and "&" in the cookie is equal to GTweb. If the string is not equal to the value, it will jump to the Baidu homepage. If the string is equal to the value, no operation will be performed.
Window. location = "www.baidu.com ";
}

}

Related Article

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.