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

一天一个js(10)cookie操作,写cookie,读取cookie,删除cookie
//写Cookie参数:CKname是cookie名称(必填),CKvalue是cookie值(必填),duration是过期时间(默认为关闭浏览器),CKpath是可访问cookie的目录(默认为所有页面),CKdomain是可访问cookie的主机名(默认为当前域名下[子域名]);
function setCookie(CKname,CKvalue,duration,CKpath,CKdomain){
	var NewDate = new Date();
	NewDate.setTime(NewDate.getTime()+duration*24*60*60*1000);
	document.cookie = CKname+"="+escape(CKvalue)+(duration?";expires="+NewDate.toGMTString():"")+(CKpath?";path="+CKpath:"")+(CKdomain?";domain="+CKdomain:"");
};
//读取指定名称的cookie
function getcookie(CKname){
	var arrCookie = document.cookie.match(new RegExp("(^| )"+CKname+"=([^;]*)(;|$)"));
	if(arrCookie!=null){
		return unescape(arrCookie[2]);
	}else{
		return null;
	};
};
//删除指定名称的cookie
function deleteCookie(CKname){
	document.cookie = CKname+"=;expires="+(new Date(0)).toGMTString();
};