日期:2014-05-16  浏览次数:20338 次

javascript 对cookie的操作封装

js 操纵 cookie

//写入cookie
function setCookie(name, value) {
var days = 30;
var exp = new Date();
exp.setTime(exp.getTime() + days * 24 * 60 * 60 * 1000);
document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString();
}
//读取cookie
function getCookie(name) {
var arr = document.cookie.match(new RegExp("(^|)" + name + "=([^;]*)(;|$)"));
return (arr != null) ? unescape(arr[2]) : null;
}
//删除cookie
function delCookie(name) {
var exp = new Date();
exp.setTime(exp.getTime() - 1);
var cval = getCookie(name);
if (cval != null) document.cookie = name + "=" + cval + ";expires=" + exp.toGMTString();
}