The examples in this article describe JavaScript settings and how to get cookies. Share to everyone for your reference, specific as follows:
1. Set Cookies
function Setcookie (Cookiename,cookievalue,cookieexpires,cookiepath)
{
cookievalue = Escape (Cookievalue); /encoded latin-1
if (cookieexpires== "")
{
var nowdate = new Date ();
Nowdate.setmonth (Nowdate.getmonth () +6);
Cookieexpires = Nowdate.togmtstring ();
}
if (cookiepath!= "")
{
cookiepath = "; Path= "+cookiepath;
}
document.cookie= cookiename+ "=" +cookievalue+ "; expires=" +cookieexpires+cookiepath;
}
2. Get cookies
function Getcookievalue (cookiename)
{
var cookievalue = document.cookie;
var cookiestartat = Cookievalue.indexof ("" +cookiename+ "=");
if (cookiestartat==-1)
{
Cookiestartat = Cookievalue.indexof (cookiename+ "=");
}
if (cookiestartat==-1)
{
cookievalue = null;
}
else
{
Cookiestartat = cookievalue.indexof ("=", Cookiestartat) +1;
Cookieendat = Cookievalue.indexof (";", Cookiestartat);
if (cookieendat==-1)
{
cookieendat = cookievalue.length;
}
Cookievalue = unescape (cookievalue.substring (Cookiestartat,cookieendat));//decode Latin-1
} return
Cookievalue;
}
Example:
Attention:
Because Google Chrome browser for security only support Online-cookie, so in the local test is not effective, you need to upload to the server to try.
More about JavaScript operation cookie related content to view the site topics: "JavaScript operation Cookie Related knowledge summary" and "jquery Cookie Operation Tips Summary"
I hope this article will help you with JavaScript programming.