Cookie代码在FireFox和Explorer中运行,但不在Chrome中运行

时间:2014-10-09 16:58:19

标签: javascript google-chrome cookies

以下Cookie制作代码适用于Firefox和资源管理器,但不适用于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和资源管理器中显示cookie,但在Chrome中完全空白; 因此我立即被重定向回登录页面。这似乎表明我的cookie编写代码无效或Chrome中的cookie已关闭?但是我检查了Chrome和单选按钮以便"允许设置本地数据"在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";
    }
}

1 个答案:

答案 0 :(得分:0)

我是javascript的新手。我研究过cookies和amp;在铬上打破了我的头。我的代码在IE上工作正常。但不是Chrome。 Chrome表示启用了Cookie。但是,我不能写出一个。版本38.0.2125.111如果你在chrome上进入“about”,你可以得到帮助。显然这是一个问题。 chrome以不同的方式处理cookie。我不明白。我还没有研究过那么多。希望我的指针有所帮助。