我正在尝试创建基于JS Cookie的新闻快讯部分。我有一切正常工作,但显示内容是否存在或不存在的cookie。我正在使用JS-Cookie脚本。显示基于Javascript 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();
});
你不设置cookie在检查它的存在之前? 'Cookies.set('emailsignup','seenemail',{expires:30,path:'/'});' – Cornwell
是的,我注意到这个错误和更新的代码。很快会发布编辑更新。 –