JS 对 Cookie简单操作

<script type="text/javascript">
    
    function getCookie(objName){//获取指定名称的cookie的值
    	 
    		    var arrStr = document.cookie.split("; ");//获取 cookie数组
    		 
    		    for(var i = 0;i < arrStr.length;i ++){
    	 
    		        var temp = arrStr[i].split("=");  // 通过 '=' 分割成  键和值
    	 
    		        if(temp[0] == objName) return unescape(temp[1]);
    		   }
    	 
    		}

    function delCookie(name)//删除cookie
    	 
    	{
    	 
    	   document.cookie = name+"=;expires="+(new Date(0)).toGMTString();
    	 
    	}


    function addCookie(objName,objValue,objHours){      //添加cookie
    		 
    		    var str = objName + "=" + escape(objValue);
    		 
    		    if(objHours > 0){    //不设定时间,浏览器关闭时cookie自动消失
    		 
    		        var date = new Date();
    		 
    		        var ms = objHours*3600*1000;
    		 
    		        date.setTime(date.getTime() + ms);
    		 
    	        str += "; expires=" + date.toGMTString();
    		 
    		   }
    	 
    	       document.cookie = str;
    		 
    		}
	alert(getCookie('Parameter'));
	delCookie('Parameter');
	alert(getCookie('Parameter'));  //此处 应该 弹出 undefine 
	addCookie('Parameter',"new value",5);
	alert(getCookie('Parameter')); //此处 弹出 'new value ' 
    </script>

你可能感兴趣的:(cookie)