Jquery 登陆页面 Cookie记住用户名密码 (可设定保留时间)
Jquery 登陆页面 Cookie记住用户名密码 (可设定保存时间)? ? _expires (typeof hours) string ?
Jquery 登陆页面 Cookie记住用户名密码 (可设定保存时间)
? ? _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.页面- //登录事件
- ? ?? ???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;
- ? ?? ?? ?? ?? ?? ?? ?? ?}
- ? ?? ?? ?? ?? ?? ?? ?? ?});
- ? ?? ?? ?? ?? ? });
复制代码
Jquery 登陆页面 Cookie记住用户名密码http://bbs.it-home.org/thread-2179-1-1.html