Web项目,要求:保存用户名和密码在Cookie中,下次登录不再重新输入
生活随笔
收集整理的這篇文章主要介紹了
Web项目,要求:保存用户名和密码在Cookie中,下次登录不再重新输入
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
設計一個實現登錄功能的Web項目,要求:保存用戶名和密碼在Cookie中,下次登錄不再重新輸入
var cookie = {};//設置
cookie.SetCookies=function(name,value,exptime){
try{
if(arguments.length == 2) return arguments.callee(name,value,30*24*60*60*1000);
var exp = new Date();
exp.setTime(exp.getTime() + exptime);
document.cookie = name + "="+ escape(value) +";expires="+ exp.toGMTString();
}
catch(e){
throw new Error("SetCookies: " + e.message);
return false;
}
}
//獲取
cookie.GetCookies=function(name){
try{
var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));
if(arr != null)
return unescape(arr[2]);
return null;
}
catch(e){
throw new Error("GetCookies: " + e.message);
return false;
}
}
//提交時函數頭省略獲取
var username = document.getElementById('username').value;
var password = document.getElementById('password').value;
cookie.SetCookies ("username",username);
cookie.SetCookies ("password",password);
//進入本頁時 jquery 加載onload 別忘了導包
$(document).ready(function(){
//可以判斷下null值。
document.getElementById('username').value = cookie.GetCookies("username");
document.getElementById('password').value = cookie.GetCookies("password");
});
轉載于:https://www.cnblogs.com/ydfq-home/p/5017351.html
總結
以上是生活随笔為你收集整理的Web项目,要求:保存用户名和密码在Cookie中,下次登录不再重新输入的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 算法九——回溯算法
- 下一篇: 泛型实现List(ListT)排序