简体   繁体   English

JavaScript不会保存Cookie

[英]Javascript won't save cookie

I'm using a quick Javascript code (based on a tutorial) to try and create an age screen on a site. 我正在使用快速的Javascript代码(基于教程)尝试在网站上创建年龄屏幕。 However, although the age screen should only pop up once and then be fine for a while, it pops up every refresh. 但是,尽管年龄屏幕应该只弹出一次,然后暂时显示一段时间,但每次刷新时都会弹出。 Here's the code I'm using: 这是我正在使用的代码:

<script type="text/javascript">
function setCookie(cname, cvalue, exdays) {
    var d = new Date();
    d.setTime(d.getTime() + (exdays*24*60*60*1000));
    var expires = "expires="+d.toGMTString();
    document.cookie = cname + "=" + cvalue + "; " + expires;
}
function getCookie(cname) {
    var name = cname + "=";
    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);
        if(c.indexOf(name) == 0) return c.substring(name.length,c.length);
    }
    return "";
}
function checkCookie() {
    var age = getCookie("allowed");
    if (age != true) {
        age = confirm("you must be 18 or older to enter and view the content on this blog");
    }
    if (age == true) {
        setCookie("allowed", age, 7);
    } else if (age == false) {
        //window.location.replace("http://tumblr.com/dashboard");
    }
    return age;
}
</script>

I've checked every available online resource and tutorial I can find, but nothing explains why this won't save the cookie properly. 我已经检查了所有可以找到的在线资源和教程,但是没有任何内容解释为什么它不能正确保存cookie。 If I open the console, it recognizes the site creating the cookie, but gives me undefined values for everything. 如果打开控制台,它将识别创建cookie的站点,但为我提供所有内容的未定义值。

If I have the console run the checkCookie() function, it returns the proper value depending on whether I clicked 'OK' or 'Cancel' but it still won't actually save that value into the cookie. 如果我让控制台运行checkCookie()函数,则它会根据我单击“确定”还是“取消”返回正确的值,但实际上仍不会将该值保存到cookie中。

Can anyone think of any reason this might be happening? 谁能想到这可能发生的任何原因?

UPDATE UPDATE

After changing the age == true checks from boolean checks into strings, it made no difference. age == true检查从布尔检查更改为字符串后,它没有任何区别。

The cookie is probably being saved. cookie可能正在保存。 But your getCookie method has a typo in it on like 6 c.substring(name,length,c.length); 但是您的getCookie方法中有一个错字,例如6 c.substring(name,length,c.length);

Instead you should have c.substring(name.length,c.length); 相反,您应该具有c.substring(name.length,c.length);

Update 更新

Change the following lines in your checkCookie method to 将您的checkCookie方法中的以下行更改为

if (age) {
    setCookie("allowed", age, 7);
}

OR 要么

if (age === true) {
    setCookie("allowed", age, 7);
}

Once the setCookie is called, refresh the browser and you should be able to see the cookie set. 调用setCookie后,刷新浏览器,您应该能够看到cookie集。

Update 2 更新2

getCookie returns a string so it should be age != "true" and confirm returns a boolean so age === true should work getCookie返回一个字符串,因此它应该是age != "true"confirm返回一个布尔值,所以age === true应该可以工作

var age = getCookie("allowed");
if (age != "true") {
    age = confirm("you must be 18 or older to enter and view the content on this blog");
}
if (age === true) {
    setCookie("allowed", age, 7);
} else if (age == false) {
    //window.location.replace("http://tumblr.com/dashboard");
}

(typeof age)是字符串,因此您可以将age != true更改为age != 'true' ,其他检查相同,也可以将其转换为布尔值,如!!age != true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM