js操作cookie实现记住密码功能

<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" οnblur="GetPwdAndChk()">  
    密  码:<input type="password" ID="txtPassword">  
    <input type="checkbox" ID="chkRememberPwd" />记住密码  
    <input type="button" OnClick="checkInCorrect()" value="进入"/>  
</form>    
</body>

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
在 Vue.js 应用程序中实现记住密码功能需要使用一些技术,如 Cookie、localStorage 或 Vuex。这里提供了一个使用 Cookie 实现记住密码功能的示例代码: 1. 在登录页的表单中添加一个“记住密码”的勾选框: ```html <template> <div> <input type="text" v-model="username" placeholder="用户名"> <input type="password" v-model="password" placeholder="密码"> <label><input type="checkbox" v-model="remember">记住密码</label> <button @click="login">登录</button> </div> </template> ``` 2. 在登录方法中判断是否勾选了“记住密码”,如果勾选了,将用户名和密码保存Cookie 中: ```js <script> import Cookies from 'js-cookie' export default { data() { return { username: '', password: '', remember: false } }, methods: { login() { // 发送登录请求,验证用户名和密码 // ... if (this.remember) { Cookies.set('username', this.username) Cookies.set('password', this.password) } } } } </script> ``` 3. 在应用程序的入口处读取 Cookie,如果存在 Cookie,则自动填充表单: ```js <script> import Cookies from 'js-cookie' export default { created() { const username = Cookies.get('username') const password = Cookies.get('password') if (username && password) { this.username = username this.password = password this.remember = true // 自动登录 this.login() } } } </script> ``` 在这个示例中,我们使用了 js-cookie 库来读写 Cookie。如果您使用的是 localStorage 或 Vuex,可以使用相应的 API 来读写数据。并且需要注意,如何保护用户的敏感信息,比如使用加密技术来保护用户的密码
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值