-3

My app is built on HTML (bootstrap), JavaScript (JQuery).

My login form has an input for user name and password. How can I persist the user name when a login fails? Cross-browser?

Thanks for any helpful tips!

4

3 回答 3

1

如果你不能使用 jQuery 插件,你可以使用普通的旧 JavaScript。

function createCookie(name,value,days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}

function eraseCookie(name) {
    createCookie(name,"",-1);
}
于 2013-08-09T20:35:13.590 回答
1

可能我的第一个首选解决方案是将其存储在与 php.ini 的会话中。这是一个很好的基本示例:

http://www.w3schools.com/php/php_sessions.asp

于 2013-08-09T20:53:29.570 回答
0

使用 jquery 将其存储在 cookie 中:

$.cookie("username",theUserName);
于 2013-08-09T20:14:06.243 回答