用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - asp代码库

Asp.Net中用JS中操作cookie

2014-12-19 作者: asp源码之家举报

[asp]代码库

<%@ 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>


网友评论    (发表评论)


发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...