js保存用户名

<head> 
<title>js COOKIE 记住帐号或密码</title>
<script language="javascript" type="text/javascript">
function checkInCorrect() //判断用户名和密码是否为空
{
if (document.getElementById('txtUserName').value=="")
{
alert('请输入用户名!')
document.getElementById('txtUserName').focus();
return false
}
if (document.getElementById('txtPassword').value=="")
{
alert('请输入密码!')
document.getElementById('txtPassword').focus();
return false
}
else
{
saveInfo();
return true;
}
}


saveInfo = function(){
try{
var isSave = document.getElementById('chkRememberPwd').checked; //保存按键是否选中
if (isSave) {
var usernm = document.getElementById('txtUserName').value;
var userpsw = document.getElementById('txtPassword').value;
if(usernm!="" && userpsw!=""){
SetCookie(usernm,userpsw);
}
}else {
SetCookie("","");
}
}catch(e){

}
}

function SetCookie(usern,psw){
var Then = new Date()
Then.setTime(Then.getTime() + 1866240000000)
document.cookie ="username=" + usern + "%%"+psw+";expires="+ Then.toGMTString() ;
}


function GetCookie(){
var nmpsd;
var nm;
var psd;
var cookieString = new String(document.cookie)
var cookieHeader = "username="
var beginPosition = cookieString.indexOf(cookieHeader)
cookieString = cookieString.substring(beginPosition);
var ends=cookieString.indexOf(";");
if (ends!=-1){
cookieString = cookieString.substring(0,ends);
}
if (beginPosition>-1){
nmpsd = cookieString.substring(cookieHeader.length);
if (nmpsd!=""){
beginPosition = nmpsd.indexOf("%%");
nm=nmpsd.substring(0,beginPosition);
psd=nmpsd.substring(beginPosition+2);
document.getElementById('txtUserName').value=nm;
document.getElementById('txtPassword').value=psd;
if(nm!="" && psd!=""){
document.forms[0].checkbox.checked = true
}
}
}
}
</script>
</head>
<body onLoad="document.getElementById('txtUserName').focus();GetCookie();">
<form>
用户名:<input type="text" ID="txtUserName" onblur="GetPwdAndChk()"> <br>
密 码:<input type="password" ID="txtPassword"> <br>
<input type="checkbox" ID="chkRememberPwd" />记住密码 <br>
<input type="button" OnClick="checkInCorrect()" value="进入"/>
</form>
</body>

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值