`
zb5594921
  • 浏览: 18686 次
社区版块
存档分类
最新评论

Jquery 登陆页面 Cookie记住用户名密码

 
阅读更多

记住用户名密码:

步骤

1.需要的Cookie.js 直接Copy用就行了 ,不需要做任何修改!

//hours为空字符串时,cookie的生存期至浏览器会话结束。hours为数字0时,建立的是一个失效的cookie,这个cookie会覆盖已经建立过的同名、同path的cookie(如果这个cookie存在)。   
function setCookie(name,value,hours,path){   
    var name = escape(name);   
    var value = escape(value);   
    var expires = new Date();   
    expires.setTime(expires.getTime() + hours*3600000);   
    path = path == "" ? "" : ";path=" + path;   
    _expires = (typeof hours) == "string" ? "" : ";expires=" + expires.toUTCString();   
    document.cookie = name + "=" + value + _expires + path;   
}   
//获取cookie值    方法
function getCookieValue(name){   
    var name = escape(name);   
    //读cookie属性,这将返回文档的所有cookie   
    var allcookies = document.cookie;          
    //查找名为name的cookie的开始位置   
    name += "=";   
    var pos = allcookies.indexOf(name);       
    //如果找到了具有该名字的cookie,那么提取并使用它的值   
    if (pos != -1){                                             //如果pos值为-1则说明搜索"version="失败   
        var start = pos + name.length;                  //cookie值开始的位置   
        var end = allcookies.indexOf(";",start);        //从cookie值开始的位置起搜索第一个";"的位置,即cookie值结尾的位置   
        if (end == -1) end = allcookies.length;        //如果end值为-1说明cookie列表里只有一个cookie   
        var value = allcookies.substring(start,end);  //提取cookie的值   
        return unescape(value);                           //对它解码         
        }      
    else return "";                                             //搜索失败,返回空字符串   
}   

2.还需要一个jquery-1.6.1.min.js ,这个大家应该都有 ,就不传了。

3.首页
<input type="text" style="width:287px; border:0;"  id="j_username" name="j_username" value="">

<input type="password"  style="width:287px; border:0;" id="j_password" name="j_password" value="" />

<input type="checkbox" id="rememberPW" name="rememberPW"/>记住密码
 
4.JS代码 ,来操作存/读Cookie

//登录事件
	function doLogin() {
		var userName = document.getElementById("username");
		var pwd = document.getElementById("password");
		if(userName.value.replace(/ /g,"")==""){
				alert("用户名不能为空!");
				userName.focus();
				return false;
			}
		if(pwd.value==""){
				alert("密码不能为空!");
				pwd.focus();
				return false;
			}
		setCookie('cookUser', userName.value, time, '/');//set 获取用户名和密码 传给cookie
		setCookie('cookPass', pwd.value, time, '/');
		document.login_form.submit();
			
		}
		//设置Cookie保存时间
		var time = 0;
		
		$(document).ready(function(){
		//获取Cookie保存的用户名和密码
		var username = getCookieValue("cookUser");
		var password = getCookieValue("cookPass");
		
		if (username !='' && password !='' ) {
			$("#username").val(username);
			$("#password").val(password);
			$("#rememberPW").attr("checked", true);
		}else 
			$("#rememberPW").attr("checked", false);
		
		$("#rememberPW").click(function(){//记住密码
			if($(this).attr("checked") == 'checked'){
			time = 60 * 60 * 60;
			}
			});
		});
网上例子很多,但太杂,给个能用的。希望能帮到大家
0
6
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics