Javascript 在不同的函数中创建 Cookie

Javascript Creating a Cookie within a different funtion

我的问题是我试图调用一个函数在另一个函数中创建 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(name) 函数

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);
}

下面的代码有效-

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'));

Fiddle Url-https://jsfiddle.net/zr6h8nf7/3/

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

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

这意味着 "assign 'Jacob' to user, assign 'password' to pass, then produce the value 'password'"(将始终被视为 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 可以替换为:

function readCookie(key) {
    return document.cookie.split(';').map(function (item) {
        return item.split('=').map(decodeURIComponent);
    }).filter(function (item) {
        return item[0] && item[0].trim() === key;
    }).map(function (item) {
        return item[1];
    })[0];
}