How to use JS to get the value of a cookie
Read all cookies belonging to the current document
var allcookies = document. Cookie;
Defines a function that is used to read a specific cookie value.
function GetCookie (cookie_name)
{
var allcookies = document. Cookie;
var cookie_pos = allcookies. indexOf (Cookie_name); Length of index
If an index is found, it represents the existence of a cookie,
Conversely, the description does not exist.
if (cookie_pos! =-1)
{
Put the Cookie_pos at the beginning of the value, just add 1 to the value.
Cookie_pos + = cookie_name.length + 1; Here I have tried it, prone to problems, so please refer to the time yourself to study it well ...
var cookie_end = allcookies. indexOf (";", Cookie_pos);
if (cookie_end = =-1)
{
cookie_end = allcookies. length;
}
var value = unescape(allcookies.substring (Cookie_pos, cookie_end)); Here you can get the value of the cookie you want ...
}
return value;
}
Calling functions
var cookie_val = getcookie("username");
How to use JS to get the value of a cookie