Asp.Net中用JS中操作cookie的方法(源码实例)

Asp.Net中用JS中操作cookie的方法(源码实例)


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="cookies.aspx.cs" Inherits="File_cookies" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <link href="../lib/ligerUI/skins/Aqua/css/ligerui-all.css" rel="stylesheet" type="text/css" />
    <style type="text/css">
    </style>

    <script type="text/javascript">
        $(function () {
            $("#listbox1").ligerListBox({
                isShowCheckBox: true, isMultiSelect: false,
                data: [
                    { text: '张三', id: '1' },
                    { text: '李四', id: '2' },
                    { text: '赵武2', id: '3' },
                    { text: '赵武3', id: '4' },
                    { text: '赵武4', id: '5' },
                    { text: '赵武5', id: '6' },
                    { text: '赵武6', id: '7' },
                    { text: '赵武7', id: '8' }
                ], valueFieldID: 'test3'
            });
        });
        function clickee() {
            var name = getCookie("res_name");
            alert(name);
        }
        function setValue() {
            var name = $("#test3").val();
            addCookie("res_name", name, 0);
            alert("添加cookie成功");
        }
        function setOther() {
            top.f_addTab(null, '查看cookie', '../cookie_view.aspx')
        }


        function addCookie(objName, objValue, objHours) {

           //判断是否已存在相同名称的cookie 存在则删除
            //if (chkcookies(objName)) {

            //    var date = new Date();
            //    date.setTime(date.getTime() - 10000);
            //    document.cookie = objName + "=" + objValue + "; expires=" + date.toGMTString();
            //}


         




            var str = objName + "=" + escape(objValue);


            //path=/";      (path是非必须的,但如果没有path=/,则在根目录下无法读取子目录下的cookies。
            //str+="; path="+"/";

             //为0时不设定过期时间,浏览器关闭时Cookie自动消失
            if (objHours > 0) {
                var date = new Date();
                var ms = objHours * 3600 * 1000;
                date.setTime(date.getTime() + ms);
                str += "; expires=" + date.toGMTString();
            }
            //添加cookie 
            document.cookie = str;
            alert("添加Cookie成功10101");
            //alert(getCookie(objName) + "------------------");
            //alert("添加Cookie成功");

        }
        function getCookie(name) {
            //获得cookie
            var bikky = document.cookie;
            name += "=";
            var i = 0;
            //如果cookie 不为空则 循环截取出 相应 名称 的cookie值
            while (i < bikky.length) {
                var offset = i + name.length;
                if (bikky.substring(i, offset) == name) {
                    var endstr = bikky.indexOf(";", offset);
                    if (endstr == -1) endstr = bikky.length;
                    return unescape(bikky.substring(offset, endstr));
                }
                i = bikky.indexOf(" ", i) + 1;
                if (i == 0) break;
            }
            return null;
        }



    </script>
</head>
<body style="padding:10px">
     <div id="listbox1"></div> 

 <div  style="margin-top:9px;"><a href="javascript:void(0)" onclick="setValue()" class="l-button" style="width: 100px">设置cookie值</a></div>
 <div  style="margin-top:4px;"><a href="javascript:void(0)" onclick="clickee()" class="l-button" style="width: 100px">获取cookie值</a></div>
 <div  style="margin-top:4px;"><a href="javascript:void(0)" onclick="setOther()" class="l-button" style="width: 100px">异面获取cookie值</a></div>

</body>
</html>

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="cookie_view.aspx.cs" Inherits="File_cookie_view" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>

    <script type="text/javascript">

        function clickee() {
            var name = getCookie("res_name");
            alert(name);
        }
        function getCookie(name) {
            //获得cookie
            var bikky = document.cookie;
            alert(bikky);
            name += "=";
            var i = 0;
            //如果cookie 不为空则 循环截取出 相应 名称 的cookie值
            while (i < bikky.length) {
                var offset = i + name.length;
                if (bikky.substring(i, offset) == name) {
                    var endstr = bikky.indexOf(";", offset);
                    if (endstr == -1) endstr = bikky.length;
                    return unescape(bikky.substring(offset, endstr));
                }
                i = bikky.indexOf(" ", i) + 1;
                if (i == 0) break;
            }
            return null;
        }





    </script>
</head>
<body>
  
   <div style="margin-top:4px;"><a href="javascript:void(0)" onclick="clickee()" class="l-button" style="width: 100px">获取cookie值</a></div>
   
</body>
</html>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

敦厚的曹操

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值