js 操作cookies 方法

js 操作cookies 方法


var str="";
//JS操作cookies方法!
//Expires		设置Cookie的有效期限
//Domain		添加域名限制
//Path			添加路径限制
//写cookies
function setCookie(name,value){
	var Days=30;//过期时间30天
	var date=new Date(); 
	date.setTime(date.getTime()+Days*24*60*60*1000);
	document.cookie=name+"="+escape(value)+";expires="+date.toGMTString();
}
//读取cookies的值
function getCookie(name){
	var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
	if(arr=document.cookie.match(reg)){
		return unescape(arr[2]);
	}
	else{
		return null;
	}
}

	function delCookie(name) {
		var exp = new Date();
		exp.setTime(exp.getTime() - 1);
		var cval = getCookie(name);
		str += "||cval:" +cval;
		if (cval != null)
		document.cookie=name+"="+escape(cval)+";expires="+exp.toGMTString();
	}

	function w() {
		var name = document.getElementById("vkey").value;
		var value = document.getElementById("vvalue").value;
		setCookie(name, value);
		str += "||get:"+getCookie(name);
		write();
		str="";
	}

	function d() {
		var name = document.getElementById("vkey").value;
		delCookie(name);
		write();
		str="";
	}
	function g() {
		var name = document.getElementById("vkey").value;
		str += "||get:"+getCookie(name);
		write();
		str="";
	}
	
	function write()
	{
		document.getElementById("info").innerHTML= ("||cookes:"+document.cookie)+" ||||" +str;
	}
</script>

你可能感兴趣的:(Cookies)