记住密码功能 JS结合JQuery 操作 Cookie 实现记住密码和用户名!

时间:2023-03-09 09:33:52
记住密码功能 JS结合JQuery 操作 Cookie 实现记住密码和用户名!
// 记住密码功能 JS结合JQuery 操作 Cookie 实现记住密码和用户名!

var username = document.getElementById("username");
var password = document.getElementById("password");
var date=new Date();
var expiresDays=1000; //过期时间。
date.setTime(date.getTime()+expiresDays*24*3600*1000);
var str_in;
var str_out = "";
var num_in;
var num_out = ""; //加密
function str_to_num(val) {
num_out = "";
//if(form.input.value == "") alert(e);
//else {
str_in = escape(val);
for(i = 0; i < str_in.length; i++) {
num_out += str_in.charCodeAt(i) - 23;
}
//form.output.value = num_out;
//form.input.value = "";
return num_out;
} //解密
function num_to_str(val) {
str_out = "";
num_out = val;
for(i = 0; i < num_out.length; i += 2) {
num_in = parseInt(num_out.substr(i,[2])) + 23;
num_in = unescape('%' + num_in.toString(16));
str_out += num_in;
}
var str_out_jm = unescape(str_out);
return str_out_jm ;
}
$(function(){
$(".data_sub.sub_click").click(function(){
if(document.getElementById("checkBox").checked){
document.cookie = "a="+ str_to_num(username.value)+"; expires=" + date.toGMTString();
;
document.cookie = "b="+ str_to_num(password.value)+"; expires=" + date.toGMTString();
}else{
return;
}
});
var user = getCookie("a");
var pass = getCookie("b");
if(username.value == "")
{
username.value = num_to_str(user);
}
if(password.value == ""){
password.value = num_to_str(pass);
}
});
}); function getCookie(name){
var strCookie=document.cookie;
var arrCookie=strCookie.split("; ");
for(var i=0;i<arrCookie.length;i++){
var arr=arrCookie[i].split("=");
if(arr[0]==name)return arr[1];
}
return "";
}