js使用cookies存取用户名和密码

//记录Cookie
function SetCookie(name, value, days){
    var finalDays = 7; //cookie存储时间默认7天
    if (typeof (days) != "undefined" && !isNaN(days)){
        finalDays = parseInt(days);
    }
    var exp = new Date();
    exp.setTime(exp.getTime() + finalDays * 24 * 60 * 60 * 1000);
    document.cookie = name + "=" + escape(value) + ";path=/ ;expires=" + exp.toGMTString();

}


//删除Cookie
function DeleteCookie(name) {
    var exp = new Date();
    exp.setTime(exp.getTime() - 1);
    var cval = GetCookie(name);
    document.cookie = name + "=" + cval + "; path=/ ;expires=" + exp.toGMTString();
}


//获取Cookie
function GetCookie(name) {
    var arg = name + "=";
    var alen = arg.length;
    var clen = document.cookie.length;
    var i = 0;
    while (i < clen) {
        var j = i + alen;
        if (document.cookie.substring(i, j) == arg)
            return GetCookieVal(j);
        i = document.cookie.indexOf(" ", i) + 1;
        if (i == 0) break;
    }
    return null;
}



//获取Cookie
function GetCookieVal(offset) {
    var endstr = document.cookie.indexOf(";", offset);
    if (endstr == -1)
        endstr = document.cookie.length;
    return unescape(document.cookie.substring(offset, endstr));
}

//初始化用户名及密码
function GetUser(userIndex) {
    var nameKey = "RecordName";
    var pwdKey = "RecordPwd";
    if (userIndex > 0) {
        nameKey += userIndex;
        pwdKey += userIndex;
    }
    var loginCode = GetCookie(nameKey);
    var pwd = GetCookie(pwdKey);
    if (loginCode != null) {
        $("#txtLoginCode").val(loginCode);
    } else {
        $("#txtLoginCode").val("");
    }
    if (pwd != null) {
        $("#chkPwd").attr("checked", true);
        $("#txtPwd").val(pwd);
    } else {
        $("#chkPwd").attr("checked", false);
        $("#txtPwd").val("");
    }
}


//记录用户名、密码及用户头像
function SaveUser(loginCode, pwd, photoData) {
    //头像
    var userIndex = 0;
    var loginType = $("#hidLoginType").val().toLowerCase();
    var photoKey = "RecordPhoto";
    if (loginType == "slide") {
        userIndex = $(".contentUser ul li.loginCurrent").index();
        if (userIndex > 0) {
            photoKey += userIndex;
        }
    }
    if (photoData) {
        SetCookie(photoKey, photoData);
    } else {
        DeleteCookie(photoKey);
    }
    //用户名、密码
    var nameKey = "RecordName";
    var pwdKey = "RecordPwd";
    if (userIndex > 0) {
        nameKey += userIndex;
        pwdKey += userIndex;
    }
    SetCookie(nameKey, loginCode);
    if ($("#chkPwd").attr("checked"))
        SetCookie(pwdKey,pwd);
    else
        DeleteCookie(pwdKey);
}


//判断浏览器是否禁用cookie
function IsAllowCookie() {
    var flag = false;
    if ($.browser.msie) {
        var cookieStr = "wb_check=kcehc_bw";
        document.cookie = cookieStr;
        if (document.cookie.indexOf(cookieStr) > -1) {
            flag = true;
            var date = new Date();
            date.setTime(date.getTime() - 1000);
            document.cookie = cookieStr + "; expires=" + date.toGMTString();
        }
    } else {
        flag = navigator.cookieEnabled;
    }
    return flag;
}



function Login() {
    if (CheckInput()) {
        var loginCode = $.trim($("#txtLoginCode").val());
        var pwd = $.trim($("#txtPwd").val());
        SetCookie(loginCode,pwd,7);
        SaveUser(loginCode,pwd,7);
        $("#spring_sexurity_login_form").submit();
    }
}

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值