Today encountered a small problem, in the invocation of the cookie, Document.cookie = ", Baidu found that because I set the cookie is not set the path of the cookie, so only in the current directory page access to cookies, I want the cookie to be accessible at the parent or even all pages, I must set the path of the cookie, PATH =/. New Encapsulated Cookie:
Set Cookie,name is the name to be saved, value is the corresponding value, Iday is the expiration
function Setcookie (name,value,iday,path) {
//Use Date Object
var Odate = new Date ();
Set Expiration Time
odate.setdate (odate.getdate () + iday);
Set the cookie
document. = name + ' = ' + value + '; expires= ' + Odate + '; Path= ' + path;
}
Invoke the cookie for subsequent operation
function GetCookie (name) {
//split cookie becomes array
document. Cookies. Split ('; ');
for (var i = 0; i < arr.length; i++) {
//continue to split Name=value
var arr2 = arr[i]. Split(' = ');
if (arr2[0] = = name) {
return arr2[1]
}
}
return '
}
//delete cookie
function Removecookie (name) {
//Set cookie expiry time to-1
setcookie (name, 1,-1)
}
This makes it possible to access cookies on all pages.