关闭

cookie,添加,删除

标签: jqueryjavascriptcookiehtml
114人阅读 评论(0) 收藏 举报
分类:

js设置cookie和读取cookie的方法

<!-- js -->
<html> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <head> 
        <script language="JavaScript" type="text/javascript"> 
            
            function addCookie(objName, objValue, objHours){//添加cookie 
                var str = objName + "=" + escape(objValue); 
                if (objHours > 0) {//为0时不设定过期时间,浏览器关闭时cookie自动消失 
                    var date = new Date(); 
                    var ms = objHours * 3600 * 1000; 
                    date.setTime(date.getTime() + ms); 
                    str += "; expires=" + date.toGMTString(); 
                } 
                document.cookie = str; 
                alert("添加cookie成功"); 
            } 
            
            function getCookie(objName){//获取指定名称的cookie的值 
                var arrStr = document.cookie.split("; "); 
                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,可以将其过期时间设定为一个过去的时间 
                var date = new Date(); 
                date.setTime(date.getTime() - 10000); 
                document.cookie = name + "=a; expires=" + date.toGMTString(); 
            } 
            
            function allCookie(){//读取所有保存的cookie字符串 
                var str = document.cookie; 
                if (str == "") { 
                    str = "没有保存任何cookie"; 
                } 
                alert(str); 
            } 
            
            function $(m, n){ 
                return document.forms[m].elements[n].value; 
            } 
            
            function add_(){ 
				var cookie_name = $("myform", "cookie_name"); 
                var cookie_value = $("myform", "cookie_value"); 
                var cookie_expireHours = $("myform", "cookie_expiresHours"); 
                addCookie(cookie_name, cookie_value, cookie_expireHours); 
            } 
            
            function get_(){ 
                var cookie_name = $("myform", "cookie_name"); 
                var cookie_value = getCookie(cookie_name); 
                alert(cookie_value); 
            } 
            
            function del_(){ 
                var cookie_name = $("myform", "cookie_name"); 
                delCookie(cookie_name); 
                alert("删除成功"); 
            } 
        </script> 
    </head> 
    <body> 
        <form name="myform"> 
            <div> 
                <label for="cookie_name"> 
                    名称 
                </label> 
                <input type="text" name="cookie_name" /> 
            </div> 
            <div> 
                <label for="cookie_value"> 
                值 
                </lable> 
                <input type="text" name="cookie_value" /> 
            </div> 
            <div> 
                <label for="cookie_expireHours"> 
                多少个小时过期 
                </lable> 
                <input type="text" name="cookie_expiresHours" /> 
            </div> 
            <div> 
                <input type="button" value="添加该cookie" onclick="add_()"/>
				<input type="button" value="读取所有cookie" onclick="allCookie()"/>
				<input type="button" value="读取该名称cookie" onclick="get_()"/>
				<input type="button" value="删除该名称cookie" onclick="del_()"/> 
            </div> 
        </form> 
</body> 
</html>

jquery设置,读取cookie

<!-- jq -->
<html> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
    <head> 
<!-- 引用easyUi的文件 -->  
<link rel="stylesheet" type="text/css" href="jquery-easyui/themes/default/easyui.css">  
<link rel="stylesheet" type="text/css" href="jquery-easyui/themes/icon.css">  
<script type="text/javascript" src="jquery-easyui/jquery.min.js"></script>  
<script type="text/javascript" src="jquery-easyui/jquery.easyui.min.js"></script>  
        <script language="JavaScript" type="text/javascript"> 
            
			//设置cookie/加上时间戳
			$.cookie("hideButton", "2",{path:"/",expirse:7});
			var button = $.cookie("hideButton");	//读取cookie
			
            function addCookie(objName, objValue, objHours){//添加cookie 
                var str = objName + "=" + escape(objValue); 
                if (objHours > 0) {//为0时不设定过期时间,浏览器关闭时cookie自动消失 
                    var date = new Date(); 
                    var ms = objHours * 3600 * 1000; 
                    date.setTime(date.getTime() + ms); 
                    str += "; expires=" + date.toGMTString(); 
                } 
                document.cookie = str; 
                alert("添加cookie成功"); 
            } 
            
            function getCookie(objName){//获取指定名称的cookie的值 
                var arrStr = document.cookie.split("; "); 
                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,可以将其过期时间设定为一个过去的时间 
                var date = new Date(); 
                date.setTime(date.getTime() - 10000); 
                document.cookie = name + "=a; expires=" + date.toGMTString(); 
            } 
            
            function allCookie(){//读取所有保存的cookie字符串 
                var str = document.cookie; 
                if (str == "") { 
                    str = "没有保存任何cookie"; 
                } 
                alert(str); 
            } 
            
            function add_(){ 
				var cookie_name = $("#cookie_name").val(); 
                var cookie_value = $("#cookie_value").val(); 
                var cookie_expireHours = $("#cookie_expiresHours").val(); 
                addCookie(cookie_name, cookie_value, cookie_expireHours); 
            } 
            
            function get_(){ 
                var cookie_name = $("#cookie_name").val(); 
                var cookie_value = getCookie(cookie_name); 
                alert(cookie_value); 
            } 
            
            function del_(){ 
                var cookie_name = $("#cookie_name").val(); 
                delCookie(cookie_name); 
                alert("删除成功"); 
            } 
        </script> 
    </head> 
    <body> 
		<div>
			<div>
				名称 :<input type="text" id="cookie_name" /> <br/>
				值 :<input type="text" id="cookie_value" /> <br/>
				多少个小时过期 :<input type="text" id="cookie_expiresHours" /> 
			</div>
            <div> 
                <input type="button" value="添加该cookie" onclick="add_()"/>
				<input type="button" value="读取所有cookie" onclick="allCookie()"/>
				<input type="button" value="读取该名称cookie" onclick="get_()"/>
				<input type="button" value="删除该名称cookie" onclick="del_()"/> 
            </div> 
		</div>
</body> 
</html>



0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:72406次
    • 积分:850
    • 等级:
    • 排名:千里之外
    • 原创:12篇
    • 转载:27篇
    • 译文:1篇
    • 评论:14条