添加只要浏览器打开就持续存在的 cookie


Add cookie that lasts as long as browser is open

嗨,当您访问我的网站时,我有一个年龄验证。当你足够大的时候,你可以通过年龄验证,但问题是,每次你再次翻到那个页面时,年龄验证会再次显示 - 它应该。在关闭浏览器之前,用户不必再次进行年龄验证。 谁能帮我?我有这个javascript,如果和cookie应该添加到其中。

if(age > 18){
COOKIE HERE
}

如果您在设置 cookie 时未指定过期值,则 cookie 将在浏览器关闭时自动删除,例如。

setcookie("TestCookie", $value);

希望对:)有所帮助

当 cookie 上没有持续时间时,默认行为是在浏览器退出时将其删除。检查示例。

if(age > 18){
    document.cookie="unrestricted=true"; 
} //This is to set the cookie after some sort of "promise" from the user that they are over 18

然后,您可以检查是否已设置cookie。

if (document.cookie.indexOf("unrestricted=true;") >= 0) {
   alert("Unrestricted access");
}

有关 cookie 的更完整教程,请查看 http://www.w3schools.com/js/js_cookies.asp