js Cookie设置和读取
//设置cookie
function setCookie (name, value, day) {
//当设置的时间等于0时,不设置expires属性,cookie在浏览器关闭后删除
var expires = day * 24 * 60 * 60 * 1000;
var exp = new Date();
exp.setTime(exp.getTime() + expires);
document.cookie = name + "=" + value + ";expires=" + exp.toUTCString()+";path=/";
};
//删除cookie
function delCookie (name) {
setCookie(name, ' ', -1);
};
//获取cookie代码
function getCookie(name) {
var arr;
var reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
if (arr = document.cookie.match(reg)) {
return arr[2];
}
else
return null;
};
function setCookie (name, value, day) {
//当设置的时间等于0时,不设置expires属性,cookie在浏览器关闭后删除
var expires = day * 24 * 60 * 60 * 1000;
var exp = new Date();
exp.setTime(exp.getTime() + expires);
document.cookie = name + "=" + value + ";expires=" + exp.toUTCString()+";path=/";
};
//删除cookie
function delCookie (name) {
setCookie(name, ' ', -1);
};
//获取cookie代码
function getCookie(name) {
var arr;
var reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
if (arr = document.cookie.match(reg)) {
return arr[2];
}
else
return null;
};