Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/450.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代码在FireFox和Explorer中工作,但在Chrome中不工作_Javascript_Google Chrome_Cookies - Fatal编程技术网

Javascript Cookie代码在FireFox和Explorer中工作,但在Chrome中不工作

Javascript Cookie代码在FireFox和Explorer中工作,但在Chrome中不工作,javascript,google-chrome,cookies,Javascript,Google Chrome,Cookies,以下cookie制作代码在Firefox和Explorer中有效,但在Chrome中无效 我已设置警报以停止脚本并验证新的Date()对象是否按预期创建。所有3个浏览器都指示正在创建新的日期(),并且在到期日前添加10分钟也按预期进行 流程:登录页面-->页面 这是用于创建cookie的登录页面代码: /* Event handler for verify button click. @param event is the event that triggered this f

以下cookie制作代码在Firefox和Explorer中有效,但在Chrome中无效

我已设置警报以停止脚本并验证新的Date()对象是否按预期创建。所有3个浏览器都指示正在创建新的日期(),并且在到期日前添加10分钟也按预期进行

流程:登录页面-->页面

这是用于创建cookie的登录页面代码:

/*
    Event handler for verify button click.

    @param event is the event that triggered this function.
*/
function checkUser(event){

    // interesting note: even though the button isn't of type submit, it submits the form.
    // this prevents the form from being submitted:
    event.preventDefault();

    // obtain user name from form 
    var name = this.form.userName.value;

    // obtain password from form 
    var password = this.form.password.value;

    // is the password valid?
    if(isValidPassword(password)){

        // create new Date object for cookie's expiration
        var expires = new Date();

        if(expires){
            alert(expires.toString());
        }

        // increase expire time by 10 minutes
        expires.setMinutes(expires.getMinutes() + 10);

        alert(expires); // Firefox & Chrome show with additional 10 minutes.

        // write userName cookie
        document.cookie = "userName=" + encodeURIComponent(name) +
            "; expires=" + expires.toUTCString();

        // write password cookie
        document.cookie = "password=" +encodeURIComponent(password) +
            "; expires=" + expires.toUTCString();       

        // password is valid, allow entry to private web page.
        location.href = "project3.html";

    }else{
        // password was not valid
        alert("Password must be at least 1 character in length.");
        this.form.password.focus();         
    }
}
然后在location.href“project3.html”中,这是检查cookie的代码。弹出警报(allCookies)并在Firefox和Explorer中显示cookies,但在Chrome中完全为空因此我立即被重定向回登录页面。这似乎表明我的cookie编写代码不起作用,或者cookie在Chrome中被关闭?但我选中了Chrome,选择了cookies内容设置下的“允许设置本地数据”单选按钮。这让我觉得我在制作Chrome不喜欢的饼干时做错了什么,你能发现它并告诉我问题出在哪里吗?非常感谢

//
//  Ensure user has a right to visit this web page.
//

//attach load event listener to window  
window.addEventListener('load', verifyPassword, false);

// attach blur event listener to window
window.addEventListener('blur', verifyPassword, false);

/*
    Event handler for window load event. Ensures user has proper credentials
    via looking for password name:value pair held in document.cookie collection.
*/
function verifyPassword(){

    // decode cookies
    var allCookies = decodeURIComponent(document.cookie);

    alert(allCookies); // Blank in Chrome; shows cookies in Firefox and Explorer.

    var password;

    // is the password cookie present?  
    if(hasCookie("password", allCookies)){

        // password cookie present, get password value 
        password = getCookie("password", allCookies);               

    }else{          
        // send user to login page 
        location.href = "project3_login.html";
    }

    // password is present, is it valid?
    if(isValidPassword(password)){
        // they get to stay     
        // cookie expires in...
    }else{
        // send user to login page 
        location.href = "project3_login.html";
    }
}

我是javascript的新手。我研究了一下饼干,在铬上摔碎了头。我的代码在IE上运行良好,但在Chrome上不行。Chrome说cookies已经启用。但是,我写不出来。版本38.0.2125.111如果您在chrome上进入“关于”,您可以获得帮助。显然这是一个问题。chrome以不同的方式处理cookies。我不明白。我还没学那么多。希望我的指针能有所帮助。

我的客户最近也遇到了同样的问题。chrome开发者工具显示cookie已经设置,但document.cookie没有显示。