JS Read Cookie method summarize _javascript Skill

Source: Internet
Author: User

The example of this article summarizes the JS read Cookie method. Share to everyone for your reference. The implementation methods are as follows:

Generally on the JS read Cookie method There are many, our following instance function is mainly to use the split function to cut, and document.cookie get all the cookies and then use for traversal of all the array, judge if the cookie is the same name, then this cookie That's what we're looking for.

Method One

Copy Code code as follows:
var acookie=document.cookie.split (";");
function Getck (sname)
{//Get 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 Two

Copy Code code as follows:
function GetCookie (objname) {//Get the value of the cookie for 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 Three
Copy Code code as follows:
function GetCookie (cookiename) {
var cookiestring = Document.cookie;
var start = Cookiestring.indexof (cookiename + ' = ');
if (start = = 1)//not 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 Four

Copy Code code 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;
}

The

wants this article to help you with your JavaScript programming.

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.