验证Cookie是否存在并存储信息

时间:2014-03-20 14:10:41

标签: javascript ajax cookies storage verify

我是Javascript / Ajax的新手,我正在尝试使用Cookie存储用户名。我有一个表格,想记住这个人的名字(id = firstName)。如果他们过去已经填写了我想在下次访问页面时在span元素中显示他/她的名字。

这就是我想用伪代码做的事情。我希望它很清楚......

如果存在cookie(称为名称),则在标记元素“span”

中显示该信息

否则

当用户填写表格时存储该Cookie

(如果重要的话,我输入了type =“submit”作为我的按钮)


@banana现在我修改了我的代码以匹配我拥有的所有标签名称和ID。但是,如果我在表单中输入名称,提交并在页面后返回,则不会显示任何内容。

function setCookie(cname, cvalue, milliseconds) {
    var d = new Date();
    d.setTime(d.getTime() + (milliseconds));
    var expires = "expires=" + d.toGMTString();
    document.cookie = cname + "=" + cvalue + "; " + expires;
}
function getCookie(cname) {
    var name = cname + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i].trim();
        if (c.indexOf(name) == 0) return c.substring(name.length, c.length);
    }
    return "";
}

document.onload = checkIfCookieExists;
function checkIfCookieExists(){
var firstNameFromCookie = getCookie('firstName');
if(firstNameFromCookie == ''){
   // cookie wasnt set

}
else{
   // cookie was set
   var display = document.getElementsByTagName('span');
   display.innerHTML = firstNameFromCookie;
}
}

function init(){
 var firstName = document.getElementById('firstName');
 var cookieExpieryTimeInMilliseconds = 1000 * 60 * 60; //1000 milliseconds * 60 seconds * 60 minutes - results in 1 hour time.
 setCookie('firstName',firstName ,cookieExpieryTimeInMilliseconds );
}

window.onload = init();

2 个答案:

答案 0 :(得分:-1)

使用它们来处理cookie。只需使用您想要的名称获取getCookie,看看您是否获得空值。

客户方:

function setCookie(cname, cvalue, milliseconds) {
        var d = new Date();
        d.setTime(d.getTime() + (milliseconds));
        var expires = "expires=" + d.toGMTString();
        document.cookie = cname + "=" + cvalue + "; " + expires;
    }
function getCookie(cname) {
        var name = cname + "=";
        var ca = document.cookie.split(';');
        for (var i = 0; i < ca.length; i++) {
            var c = ca[i].trim();
            if (c.indexOf(name) == 0) return c.substring(name.length, c.length);
        }
        return "";
    }

编辑:

document.onload = checkIfCookieExists;
function checkIfCookieExists(){
    var firstNameFromCookie = getCookie('first_name');
    if(firstNameFromCookie == ''){
       // cookie wasnt set

    }
    else{
       // cookie was set
       var yourSpan = document.getElementById('yourSpansID');
       yourSpan.innerHTML = firstNameFromCookie;
    }
}

为了使用上述功能保存cookie,您只需执行以下操作:

function whatever_Function_You_Use_To_Submit_Users_Details(){
     var firstName = ... whatever you do to retrieve first name after user inserted in
     var cookieExpieryTimeInMilliseconds = 1000 * 60 * 60; //1000 milliseconds * 60 seconds * 60 minutes - results in 1 hour time.
     setCookie('first_name',firstName ,cookieExpieryTimeInMilliseconds );
}

答案 1 :(得分:-1)

要阅读Cookie,您可以使用 Zeus.js 的内置zeus.getCookie()功能。
zeus.getCookie()的作用如下:

  • 所有Cookie都会被压缩成一个字符串document.cookie
  • zeus.getCookie()从字符串
  • 中选出所需的密钥
  • 返回一个字符串。

例如,如果您设置了Cookie document.cookie = "like=trains",则可以使用zeus.getCookie("like")返回值trains(作为字符串)。

要使用 Zeus.js ,只需输入

即可
<script src="https://rawgithub.com/thetakeaway/zeus.js/master/zeus.js"></script>
<head></head>中的

干杯!