设置cookie

<html>
<meta name="name" content="content" charset='utf-8'>
<head>
<script type="text/javascript">
	function setcook(cname,cvalue,exp){
		var time=new Date();
		time.setDate(time+exp*24*60*60*1000);
		document.cookie='cname'+escape(cvalue)+((exp==null)? '':';expires='+time.toGMTString()+';');
	}
	function getcook(cname){
		if(document.cookie.length>0){
			var c_start=document.cookie.indexOf(cname+'=');
			if(c_start!=-1){
				c_start=c_start+cname.length+1;
			}
			var c_end=document.cookie.indexOf(';',c_start);
			if(c_end==-1){
				c_end=cname.length;
				}
			return unescape(document.cookie.substring(c_start,c_end));
			
		}
		return '';
	}
	function checkcook(){
		usname=getcook('usname');
		if(usname!=null&&usname!=''){
			alert('欢迎回来'+usname);
		}
		else{
			usname=prompt('请输入名字:','');
			if(usname!=null&&usname!=''){
				setcook('usname',usname,-1);
			}
		}
	}
</script>
</head>
<body onLoad="checkcook()">
</body>
</html>








//正则获取cookie
function getCookie(name) {
            var arr = document.cookie.match(new RegExp("(^| )" + name + "=([^;]*)(;|$)"));
            if (arr != null) {
                return unescape(arr[2])
            }
            return null
        }

  

posted @ 2016-04-19 14:21  honely1314  阅读(101)  评论(0编辑  收藏  举报