我正在尝试创建一个基于 JS cookie 的时事通讯部分。我的一切工作正常,但无论 cookie 是否存在都显示内容。我正在使用 JS-cookie 脚本。
我希望仅在用户尚未设置 cookie 时才显示此部分。基本上,如果这是他们第一次查看网站或 cookie 过期(30 天)之后。
现在,即使我使用私人浏览器,不同的计算机等,它一直说 cookie 已设置并显示内容。我当前的代码在未设置时有它,因此除非删除 ! 从 if 语句。
我正在使用 js-cookie 作为 cookie。你可以看到codepen http://codepen.io/byoungz/pen/YqoxJy
var emailSignup = document.getElementById("tgs-subscribe-bar"),
screenWidth = Math.max(document.documentElement.clientWidth, window.innerWidth || 0);
$('#tgs-subscribe-bar').hide();
if (Cookies.get('emailsignup') == 'null') {
if (screenWidth < 768) {
//code for smaller screens
setTimeout(function() {
$('#tgs-subscribe-bar').slideDown();
}, 4000);
} else {
//code for larger
setTimeout(function() {
$('#tgs-subscribe-bar').slideDown();
}, 1000);
}
Cookies.set('emailsignup', 'seenemail', { expires: 30, path: '/' });
}
$('.email-exit').click(function() {
$("#tgs-subscribe-bar").slideToggle();
});