美文网首页前端成神之路
cookie的操作和封装的方法

cookie的操作和封装的方法

作者: 吴佳浩 | 来源:发表于2018-03-15 00:02 被阅读4次

    前台cookie的操作 复制代码

    <head runat="server">
        <title></title>
        <script language="JavaScript" type="text/javascript">
            function addCookie(objName, objValue, objHours) { //添加cookie 
                //escape() 函数可对字符串进行编码,
                var str = objName + "=" + escape(objValue);
                if (objHours > 0) { //为0时不设定过期时间,浏览器关闭时cookie自动消失 
                    var date = new Date();
                    var ms = objHours * 3600 * 1000;
                    date.setTime(date.getTime() + ms);
                    //expires指定了coolie的生命期
                    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 = document.getElementById("cookie_name").value;
                var cookie_value = document.getElementById("cookie_value").value;
                var cookie_expireHours = document.getElementById("cookie_expiresHours").value;
                addCookie(cookie_name, cookie_value, cookie_expireHours);
            }
    
            function get_() {
                var cookie_name = document.getElementById("cookie_name").value;
                var cookie_value = getCookie(cookie_name);
                alert(cookie_value);
            }
    
            function del_() {
                var cookie_name = document.getElementById("cookie_name").value;
                delCookie(cookie_name);
                alert("删除成功");
            }
        </script>
    </head>
    
    <body>
        <form name="myform" runat="server">
            <div>
                <label for="cookie_name">
                    名称
                </label>
                <input type="text" id="cookie_name" name="cookie_name" runat="server" />
            </div>
            <div>
                <label for="cookie_value">
                    值 </lable>
                    <input type="text" id="cookie_value" name="cookie_value" />
            </div>
            <div>
                <label for="cookie_expireHours">
                    多少个小时过期 </lable>
                    <input type="text" id="cookie_expiresHours" 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_()" />
                <asp:Button ID="Button1" runat="server" Text="后台读取前台cookie" onclick="Button1_Click1" />
            </div>
        </form>
    </body>
    
    </html>
    

    相关文章

      网友评论

        本文标题:cookie的操作和封装的方法

        本文链接:https://www.haomeiwen.com/subject/chccqftx.html