js cookie 读、写...
时间:2010-08-10 来源:hl1991_2009
(function() {
var cookie = function() {
var days = 300,
exp = new Date(),
leng = arguments.length,
val = '';
exp.setTime(exp.getTime() + days*24*60*60*1000);
if (leng == 1) {
if (typeof arguments[0] == 'object') {
for (var key in arguments[0]) {
val = '';
val = key + '=' + escape(arguments[0][key]) + ';expires=' + exp.toGMTString();
document.cookie = val;
}
return;
} else {
var arr,
reg = new RegExp("(^| )"+arguments[0]+"=([^;]*)(;|$)");
if(arr = document.cookie.match(reg)) {
return unescape(arr[2]);
} else {
return null;
}
}
} else if (leng == 2) {
val = arguments[0] + '=' + escape(arguments[1]) + ';expires=' + exp.toGMTString();
document.cookie = val;
return;
} else {
return;
}
}
cookie = window.cookie || {}
}) ()