//coder:LinQifo 2007.11.5 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" ><head runat="server"> <title>js cookie Demo 记住用户名和密码</title> <script>function rememberUser()...{ //userName1=userName;Pwd=password if(document.cookie !="") ...{ alert( getCookie('userName')); alert( getCookie('password')); //下面这两句就是用来记住用户名和密码了// document.getElementById ('TextBox1').value=getCookie('userName');// document.getElementById ('TextBox2').value=getCookie('password'); }}// 用正则表达式将前后空格,用空字符串替代(PS:因为js没有现成的trim,所以要加上这个)String.prototype.trim = function() ...{ return this.replace(/(^s*)|(s*$)/g, ""); }function getCookie(objName)//获取指定名称的cookie的值...{ var arrStr = document.cookie.split(";"); for(var i = 0;i < arrStr.length;i++) ...{ var temp = arrStr[i].split("="); if(objName.trim()==temp[0].trim()) //此处如果没有去掉字符串空格就不行,偶在这里折腾了半死,主要是这种错误不好跟踪啊 ...{ return temp[1]; } }}function setCookie()//设置cookie...{if((document.getElementById ('TextBox1').value!="")&&(document.getElementById ('TextBox2').value!=""))...{ document.cookie ="userName="+document.getElementById ('TextBox1').value; document.cookie ="password="+document.getElementById ('TextBox2').value; }}</script></head><body onload =rememberUser()> <form id="form1" runat="server"> <div> <asp:TextBox ID="TextBox1" runat="server" Width="120px" EnableViewState="False"></asp:TextBox> <br /> <br /> <asp:TextBox ID="TextBox2" runat="server" Width="120px" EnableViewState="False"></asp:TextBox><br /> <br /> <asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="setCookie()"/></div> </form></body></html>