jQuery设置cookie首次访问 [英] jQuery set cookie for first visit
本文介绍了jQuery设置cookie首次访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个类似的脚本(我使用 jQuery cookie js 设置Cookie )在首次访问时显示图层。
I got a script like that (I use jQuery cookie js to set cookies) to display layer on first visit.
<script type="text/javascript">
$(document).ready(function() {
var visited = $.cookie('visited', 'yes', { expires: 1, path: '/' });
if (visited == null) {
$('.newsletter_layer').show();
$.cookie('visited', 'yes');
alert($.cookie("visited"));
}
});
</script>
不幸的是有些东西不工作。我认为if语句错误。
Unfortunatelly something is not working. I think something is wrong with the if statement. Anyone have idea what can be wrong?
推荐答案
因为你创建的cookie,它永远不会为null。你需要改变你的逻辑,首先检查是否存在cookie。如果没有,请显示 .newsletter_layer
元素,然后设置cookie值:
Because you are creating the cookie, it will never be null. You need to change your logic to first check if the cookie exists. If not, show the .newsletter_layer
element, then set the cookie value:
<script type="text/javascript">
$(document).ready(function() {
// check cookie
var visited = $.cookie("visited")
if (visited == null) {
$('.newsletter_layer').show();
alert($.cookie("visited"));
}
// set cookie
$.cookie('visited', 'yes', { expires: 1, path: '/' });
});
</script>
这篇关于jQuery设置cookie首次访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文