Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript在不同功能中创建Cookie_Javascript_Html_Function_Cookies - Fatal编程技术网

Javascript在不同功能中创建Cookie

Javascript在不同功能中创建Cookie,javascript,html,function,cookies,Javascript,Html,Function,Cookies,我的问题是我试图调用一个函数来在另一个函数中创建cookie。由于某些原因,createCookie函数不执行 在其中调用createCookie(名称)的函数 createCookie(name)函数 当我尝试使用这些函数查找值时,它显示为null function readCookie(name) { var nameEQ = name + "="; var ca = document.cookie.split(';'); for(var i=0;i < ca.

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

在其中调用createCookie(名称)的函数

createCookie(name)函数

当我尝试使用这些函数查找值时,它显示为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);
}
函数readCookie(名称){
变量nameEQ=name+“=”;
var ca=document.cookie.split(“;”);
对于(变量i=0;i
以下代码正在运行-

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-

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

if(user='Jacob',pass='password') {
这意味着“将
'Jacob'
分配给
用户
,将
'password'
分配给
密码
,然后生成值
'password'
”(该值将始终被视为

您需要的是:

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

这并不是问题所在reason@Jacob,上面的代码正在工作,我在fiddle中对此进行了测试并提供了链接,请检查此代码是否也适用于您。这不是我正在寻找的。我想在按下按钮且条件为真时创建cookie。虽然代码中的条件为真,但creatCooke函数被忽略。谢谢您的帮助。谢谢您的帮助。
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);
}
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];
}