cookie 设置、读取、删除
作者:chrispy
//设置cookie
document.cookie="cookieCaseList="+$('input').val() + ";path=/";
//获取cookie 方法
function getCookie(key){
var value = '';
var arr, reg = new RegExp('(^| )'+key+'=([^;]*)(;|$)');
if (arr = document.cookie.match(reg)) {
value = unescape(arr[2]);
}
return value;
}
//删除cookies
function clearCookie(){
var keys=document.cookie.match(/[^ =;]+(?==)/g);
if (keys) {
for (var i = keys.length; i--;) {
document.cookie=keys[i]+'="";expires=' + new Date(0).toUTCString() + ";path=/";
}
}
return true;
}
//删除指定 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() + ";path=/";
}
//保留指定cookie 其余都清除
function clearCookie(){
var keys=document.cookie.match(/[^ =;]+(?==)/g);
if (keys) {
for (var i = keys.length; i--;) {
if(keys[i] == 'isTime'){
continue;
}
document.cookie=keys[i]+'="";expires=' + new Date(0).toUTCString() + ";path=/";
}
}
return true;
}
//过期时间 2分钟
var exp = new Date();
exp.setTime(exp.getTime() + 60 * 2000);//过期时间 2分钟
document.cookie = "CuiJiText=" + CuiJiTextVal + ";expires=" + exp.toGMTString() + ";path=/";
//读取cookie
var caseIdVal = '';
var arr, reg = new RegExp("(^| )ceshi=([^;]*)(;|$)");
if (arr = document.cookie.match(reg)) {
caseIdVal = unescape(arr[2]);
}
if(caseIdVal == ''){
document.cookie = "csIsTime=" + '1' + ";expires=" + exp.toGMTString() + ";path=/";
alert(11);
}
-