登录时的"记住我"

 当我们在做各个系统的登录界面时,喜欢在加上一个功能就是“记住我”,

我用js来实现一下看看

 

function SetCookie(name, value, expires, path, domain, secure) {          // 设置cookies

    var today = new Date();

    today.setTime(today.getTime());

    if (expires) {

        expires *= 2592000;

    }

    var expires_date = new Date(today.getTime() + (expires));

    document.cookie = name + "=" + escape(value) + (expires ? ";expires=" + expires_date.toGMTString() : "" ) + (path ? ";path=" + path: "" ) + (domain ? ";domain=" + domain: "" ) + (secure ? ";secure" : "" );

}

function GetCookie(name) {                        // 获得Cookies

    var cookies = document.cookie.split(';' );

    var cookie = '' ;

    for (var i = 0; i < cookies.length; i++) {

        cookie = cookies[i].split('=' );

        if (cookie[0].replace(/^/s+|/s+$/g, '' ) == name) {

            return (cookie.length <= 1) ? "" : unescape(cookie[1].replace(/^/s+|/s+$/g, '' ));

        }

    }

    return null ;

}

// 删除Cookies

function Delcookie(name, path, domain) {

    document.cookie = name + "=" + (path ? ";path=" + path: "" ) + (domain ? ";domain=" + domain: "" ) +";expires=Thu, 01-Jan-1970 00:00:01 GMT" ;

}

if (document.getElementById("RememberMe" ).checked) {

    SetCookie('user_name' , document.getElementById("txtUserName" ).value, 1); // 保存密码一个月

    SetCookie('user_pwd' , document.getElementById("txtPassword" ).value, 1);

}

if (GetCookie('user_name' ) != null && GetCookie('user_pwd' ) != null ) // 设置记住密码的登录页面

{

    document.getElementById("txtUserName" ).value = GetCookie('user_name' );

    document.getElementById("txtPassword" ).value = GetCookie('user_pwd' );

}

 

posted @ 2015-01-30 13:46  三小  阅读(584)  评论(0编辑  收藏  举报