Cookie代码可在FireFox和Explorer中使用,但不能在Chrome中使用

Cookie code working in FireFox and Explorer but not Chrome

本文关键字:Chrome 但不能 Explorer 代码 FireFox Cookie      更新时间:2023-09-26

以下cookie制作代码适用于Firefox和Explorer,但不适用于Chrome。

我已经设置了警报来停止脚本,并验证新的Date()对象是否按预期创建。所有3个浏览器都指示正在创建新的Date(),并且在到期日前添加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中弹出并显示Cookie,但在Chrome中完全为空所以我会立即被重定向回登录页面。这似乎表明要么我的cookie编写代码不工作,要么cookie在Chrome中被关闭?但我检查了Chrome浏览器,并选择了Cookie内容设置下的"允许设置本地数据"单选按钮。这让我觉得我在制作cookie时做错了什么,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表示已启用cookie。但是,我写不出来。版本38.0.2125.111如果你在chrome上进入"about",你可以得到帮助。显然这是一个问题。chrome以不同的方式处理cookie。我不明白。我还没学那么多。希望我的指针能有所帮助。

相关文章: