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.