标签:des style blog http java 文件
HttpCookie cookie = new HttpCookie("MyOnlyCookieName");//定义自己标示性的Cookie名称然后对创建的HttpCookie对象的" Value" 属性分配一个字符串值," Value" 的值就是后来产生的Cookie的值。
cookie [ " 姓名" ] = " 王天"; cookie [ " 性别" ] = " 男"; cookie [ " 年龄" ] = " 26";Cookie有临时的,也有永远的。永久 Cookie 以文件形式存储在计算机上,关闭 Internet Explorer 时仍然保留在计算机上。再次访问该站点时,创建该 Cookie 的网站可以读取。在具体的编程时候,是在写入此Cookie的时候,设定Cookie的生命周期,具体如下代码:
public partial class cookieTest: System.Web.UI.Page { string CookieKye = "jinWebCookies"; protected void Page_Load(object sender, EventArgs e) { } protected void WriteCookie() { CookieKye = readCookie(); HttpCookie cookie = new HttpCookie(CookieKye);//定义cookie对象以及名为Info的项 DateTime dt = DateTime.Now;//定义时间对象 TimeSpan ts = new TimeSpan(30, 0, 0, 0);//cookie有效作用时间. cookie.Expires = dt.Add(ts);//添加作用时间 string searchKey=searchId.Text.Trim(); cookie.Values.Add(searchKey, searchKey);//增加属性 Response.AppendCookie(cookie);//确定写入cookie中 } private string readCookie() { int i=0; while (i >= 0) { if (Request.Cookies[CookieKye+i] == null) { return CookieKye + i; } i++; } return ""; } protected void demoCookie() { //读取cookie if (Request.Cookies[CookieKye] != null) { string temp = Convert.ToString(Request.Cookies["Info"].Value); if (temp == "") { Response.Write("空"); } else Response.Write(temp); } else { Response.Write("error"); } //修改cookie Response.Cookies["Info"]["user"] = "2"; Response.Cookies["Info"].Expires = DateTime.Now.AddDays(1); //删除cookie下的属性 HttpCookie acookie = Request.Cookies["Info"]; acookie.Values.Remove("userid"); acookie.Expires = DateTime.Now.AddDays(1); Response.Cookies.Add(acookie); //删除所有cookie,就是设置过期时间为现在就行了 int limit = Request.Cookies.Count - 1; for (int i = 0; i < limit; i++) { acookie = Request.Cookies[i]; acookie.Expires = DateTime.Now.AddDays(-1); Response.Cookies.Add(acookie); } } protected void Unnamed1_Click(object sender, EventArgs e) { WriteCookie(); }
<script> var setSearchFlag; function showSearch(obj) { clearSearchFlag(); var w3c = (document.getElementById) ? true : false; //w3c 标准 var ns6 = (w3c && (navigator.appName == "Netscape")) ? true : false; //Netscape浏览器 标准的W3C var left, top; if (!ns6) {//判断IE var nLt = 0; var nTp = 0; var offsetParent = obj; while (offsetParent != null && offsetParent != document.body) { nLt += offsetParent.offsetLeft; nTp += offsetParent.offsetTop; offsetParent = offsetParent.offsetParent; } left = nLt; top = nTp + obj.offsetHeight; } else {//标准w3c left = obj.offsetLeft - 5; top = obj.offsetTop + obj.offsetHeight; } $('#showInfo').css('display', 'block'); $('#showInfo').css('top', top); var seach = getCookie("jinWebCookies"); $('#showInfo').html(seach); } function hideSearch(obj) { setSearchFlag = setTimeout(function () { $('#showInfo').css('display', 'none'); }, 100); } function clearSearchFlag() { window.clearTimeout(setSearchFlag); //取消由setTimeout()方法设置的定时器。 } function getCookie(cookieName) {//读取cookie var cookieContent = ''; var cookieAry = document.cookie.split("; "); //得到Cookie数组 for (var i = 0; i < cookieAry.length; i++) { //var cookieName = cookieName + i; var temp = cookieAry[i].split("="); if (temp[0] == cookieName) { cookieContent = unescape(temp[1]); } } return cookieContent; } //写入Cookie //document.cookie = "cookieName=" + escape("要写入的内容"); //cookieName为要写入的Cookie的名称 </script>ok,然后是插入一个搜索框,然后触发js
<form id="form1" runat="server"> <asp:TextBox runat="server" id="searchId" onkeyup="showSearch(this)" onblur="hideSearch(this)"/> <asp:Button runat="server" Text="btn_search AddCookie" onclick="Unnamed1_Click" /> </form> <div style="width:150px;height:300px;border:1px solid #817F82;display:none;position: absolute;" id="showInfo"></div>以上就是本人对此处cookie保存用户搜索数据,然后在用户输入搜索数据弹出提示做出的一些实验。总体方法就是这样,有兴趣的同学可以一起讨论,希望能有一个更有效的实现方式。
浅谈asp.net通过本机cookie仿百度(google)实现搜索input框自动弹出搜索提示,布布扣,bubuko.com
浅谈asp.net通过本机cookie仿百度(google)实现搜索input框自动弹出搜索提示
标签:des style blog http java 文件
原文地址:http://blog.csdn.net/jintougao/article/details/37593255