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