JavaScript(JS)记住用户名和密码

// 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 >
 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值