Javascript在不同的函数中创建Cookie

时间:2015-03-03 03:43:54

标签: javascript html5 function cookies

我的问题是我试图调用一个函数在不同的函数中创建一个cookie。由于某种原因,createCookie函数不会执行。

我调用createCookie(name)的函数

function login() {
    var user = document.getElementById('name').value;
    var pass = document.getElementById('pass').value;
        if(user='Jacob',pass='password') {
        createCookie('name','Jacob',0);
        }else{
        alert('Invalid Credentials');
    }
}

createCookie(名称)函数

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=/";
}

当我尝试使用这些函数找到值时,它显示为null。

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 display(){
    var x = readCookie('name');
    alert(x);
}

2 个答案:

答案 0 :(得分:0)

以下代码正常运作 -

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

    }
    else {
        expires = "";
    }
    document.cookie = name + "=" + value + expires + "; path=/";
}

function getCookie(c_name) {
    if (document.cookie.length > 0) {
        c_start = document.cookie.indexOf(c_name + "=");
        if (c_start != -1) {
            c_start = c_start + c_name.length + 1;
            c_end = document.cookie.indexOf(";", c_start);
            if (c_end == -1) {
                c_end = document.cookie.length;
            }
            return unescape(document.cookie.substring(c_start, c_end));
        }
    }
    return "";
}
createCookie('username','Jacob',0);
alert(getCookie('username'));

小提琴手 - https://jsfiddle.net/zr6h8nf7/3/

答案 1 :(得分:0)

据我所知,您的代码存储并检索cookie很好,但代码中存在一个大错误:

if(user='Jacob',pass='password') {

这意味着“将'Jacob'分配给user,将'password'分配给pass,然后生成值'password'”(将始终视为{ {1}})。

您需要的是:

true

另外,不要吝啬花括号。它们中有很多可以绕过,如果没有它们,你的代码就会变得混乱。以下似乎工作正常:

if(user === 'Jacob' && pass === 'password') {

另请注意,function login() { var user = document.getElementById('name').value; var pass = document.getElementById('pass').value; if (user === 'Jacob' && pass === 'password') { createCookie('name', 'Jacob', 0); } else { alert('Invalid Credentials'); } } function createCookie(key, 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 = key + "=" + value + expires + "; path=/"; } function readCookie(key) { var keyEq = key + "="; 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 display() { var x = readCookie('name'); alert(x); } 可以替换为:

readCookie