设计一个实现登录功能的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");
});
//设置
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");
});
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |