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

作者:北京诚信汇发物流有限公司  来源:www.cxhfwL.com  发布时间:2017-09-11 13:28:30
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>

,站群,站群

企业建站2800元起,携手武汉肥猫科技,做一个有见地的颜值派!更多优惠请戳:上海网站建设 http://www.flpsz.com


上一篇:Python编码问题详解
下一篇:最后一页