//coder:LinQifo 2007.11.5( Fall in love with LYF Secretly)
<%@ 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>