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> </pre><pre class="brush:javascript; toolbar: true; auto-links: false;"> <%@ 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> </pre>
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!