控制台写入cookie
document.cookie='name=val'
方法:
写cookie
function addCookie(name, value, expiresHours) {//expiresHours过期时间(小时)
var cookieString = name + "=" + escape(value);
if (expiresHours > 0) {
var date = new Date();
date.setTime(date.getTime + expiresHours * 3600 * 1000);
cookieString = cookieString + "; expires=" + date.toGMTString();
}
document.cookie = cookieString;
}
读cookie
function getCookie(name) {
var strCookie = document.cookie;
var arrCookie = strCookie.split("; ");
for (var i = 0; i < arrCookie.length; i++) {
var arr = arrCookie[i].split("=");
if (arr[0] == name) {
return unescape(arr[1]);
} else {
return "";
}
}
}
删除cookie(设定一个过去的时间即可)
function deleteCookie(name) {
var date = new Date();
date.setTime(date.getTime() - 10000);
document.cookie = name + "=v; expires=" + date.toGMTString();
}