cookie不能再本地操作闸与;操作之前可以啟動一個本地服務(wù)
js
//用window的onload事件吮螺,窗體加載完畢的時候
window.onload=function(){
//獲取cookie
let cusername = SubCookieUtil.getAll('zxs'),
user = document.querySelector('#user'),
password = document.querySelector('#password'),
ckd = document.querySelector('.layui-form-checkbox');
if(cusername !== null){
user.value = cusername.account;
password.value = cusername.password;
ckd.className += ' layui-form-checked'
}
}
var SubCookieUtil = {
/** 設(shè)置一條完整的cookie
* param name : 表示cookie的名稱妙黍,必填
* param subCookies : 表示cookie的值拂募,為一個對象腰埂,必填
* param expires : 表示cookie的過期時間耕肩,可以不填
* param domain : 表示cookie的域名八孝,可以不填
* param path : 表示cookie的路徑插掂,可以不填
* param secure : 表示cookie的安全標志应役,可以不填
* eg : SubCookieUtil.setAll("info", { name : "zhang", age : 23});
**/
setAll : function (name, subCookies, expires, domain, path, secure) {
let date = new Date();
var cookieText = "", subName, cookieParts = [];
cookieText += encodeURIComponent(name) + "=";
for(subName in subCookies) {
cookieParts.push(encodeURIComponent(subName) + "=" + encodeURIComponent(subCookies[subName]));
}
if (cookieParts.length > 0) {
cookieText += cookieParts.join("&");
if (expires instanceof Date) {
cookieText += "; expires=" + expires.toGMTString();
}
if (path) {
cookieText += "; path=" + path;
}
if (domain) {
cookieText += "; domain=" + domain;
}
if (secure) {
cookieText += "; secure";
}
} else {
cookieText += "; expires=" + date.toGMTString();
}
document.cookie = cookieText;
console.log(document.cookie)
},
/** 設(shè)置一條子cookie
* param name : 表示cookie的名稱,必填
* param subName : 表示子cookie的名稱燥筷,必填
* param value : 表示子cookie的值箩祥,必填
* param expires : 表示cookie的過期時間,可以不填
* param domain : 表示cookie的域名肆氓,可以不填
* param path : 表示cookie的路徑袍祖,可以不填
* param secure : 表示cookie的安全標志,可以不填
* eg : SubCookieUtil.set("info", "sex", "boy");
**/
set : function (name, subName, value, expires, domain, path, secure) {
var cookies = this.getAll(name) || {};
cookies[subName] = value;
this.setAll(name, cookies, expires, domain, path, secure);
},
/** 讀取一條完整cookie
* param name : 表示cookie的名稱谢揪,必填
* return : 一個cookie對象
* eg : SubCookieUtil.getAll("info");
**/
getAll : function (name) {
var cookieName = encodeURIComponent(name) + "=",
cookieStart = document.cookie.indexOf(cookieName),
cookieValue = "", i, len, subCookies, parts, result = {};
if (cookieStart > -1) {
var cookieEnd = document.cookie.indexOf (";", cookieStart);
if (cookieEnd == -1) {
cookieEnd = document.cookie.length;
}
cookieValue = decodeURIComponent(document.cookie.substring(cookieStart + cookieName.length, cookieEnd));
if (cookieValue.length > 0) {
subCookies = cookieValue.split("&");
for (i = 0, len = subCookies.length; i < len; i++) {
parts = subCookies[i].split("=");
result[decodeURIComponent(parts[0])] = decodeURIComponent(parts[1]);
}
return result;
}
}
return null;
},
/** 獲取一條子cookie的值
* param name : 表示cookie的名稱蕉陋,必填
* param subName : 表示子cookie的名稱
* return : 一個子cookie的值
* eg : SubCookieUtil.get("info", "name");
**/
get : function (name, subName) {
var cookies = this.getAll(name);
if (cookies) {
return cookies[subName];
} else {
return null;
}
},
/** 刪除一條完整cookie
* param name : 表示cookie的名稱,必填
* param domain : 表示cookie的域名拨扶,可以不填
* param path : 表示cookie的路徑凳鬓,可以不填
* param secure : 表示cookie的安全標志,可以不填
* eg : SubCookieUtil.unsetAll("info");
**/
unsetAll : function (name, domain, path, secure) {
let d = new Date() ;
this.setAll(name, "", d.toGMTString(), domain, path, secure);
},
/** 刪除一條子cookie
* param name : 表示cookie的名稱患民,必填
* param subName : 表示子cookie的名稱缩举,必填
* param domain : 表示cookie的域名,可以不填
* param path : 表示cookie的路徑匹颤,可以不填
* param secure : 表示cookie的安全標志仅孩,可以不填
* eg : SubCookieUtil.unset("info", "name");
**/
unset : function (name, subName, domain, path, secure) {
var cookies = this.getAll(name);
if (cookies) {
delete cookies[subName];
this.setAll(name, cookies, null, domain, path, secure);
}
}
};