设置一个 jQuery cookie 只显示一次弹出窗口 [英] Set a jQuery cookie to show popup only once
问题描述
我是 jQuery 的绝对新手.我正在学习,但有一条圣诞节消息,我需要立即启动并运行.
I'm an absolute newbie in jQuery. I'm learning, but there is a Christmas message that I need to get up and running within no time.
我已将这些包含在页面的标题中:
I've included these in the header of the page:
<script type="text/javascript" src="scripts/jquery-1.7.min.js"></script>
<script type="text/javascript" src="scripts/jquery.cookies.2.2.0.min.js"></script>`
然后使用 jQuery 弹出窗口跟随消息.这是:
Then follows the message using a jQuery popup. Here it is:
<script type="text/javascript">
$(document).ready(function() {
var id = '#dialog';
//Get the screen height and width
var maskHeight = $(document).height();
var maskWidth = $(window).width();
//Set height and width to mask to fill up the whole screen
$('#mask').css({'width':maskWidth,'height':maskHeight});
//transition effect
$('#mask').fadeIn(1000);
$('#mask').fadeTo("slow",0.7);
//Get the window height and width
var winH = $(window).height();
var winW = $(window).width();
//Set the popup window to center
$(id).css('top', winH/2-$(id).height()/2);
$(id).css('left', winW/2-$(id).width()/2-220);
//transition effect
$(id).fadeIn(2000);
//if close button is clicked
$('.window .close').click(function (e) {
//Cancel the link behavior
e.preventDefault();
$('#mask').hide();
$('.window').hide();
});
//if mask is clicked
$('#mask').click(function () {
$(this).hide();
$('.window').hide();
});
});
</script>
在 body
中,我输入了以下信息:
In the body
I've put the message:
<div style="top: 199.5px; left: 200px; display: none;" id="dialog" class="window">
XMAS MESSAGE
<a href="#" class="close">Shut this popup.</a>
</div>
到目前为止一切顺利.下一步是不要一遍又一遍地用相同的信息让我的回访者感到厌烦(推迟六十天就足够了).
So I want to set a cookie using the jQuery cookie plug-in:
所以我想使用jQuery cookie插件设置一个cookie:
function setCookie() {
$.cookie('test_status', '1', { path: '/', expires: 60 });
return false;
}
Which is then found the next time a visitor hits the same page and the Christmas message is not shown till the message expires.
然后在访问者下次访问同一页面时找到它,并且直到消息过期才会显示圣诞节消息.
Now if-else
statements are the higher kind of jQuery I'm not familiar with just yet. So, could anyone explain it to me?
现在 if-else
语句是我还不熟悉的高级 jQuery.那么,谁能给我解释一下?
推荐答案
这样的事情可能会有所帮助:
$(document).ready(function(){ if ($.cookie('test_status') != '1') { //show popup here $.cookie('test_status', '1', { expires: 60}); } });
这篇关于设置一个 jQuery cookie 只显示一次弹出窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!