Pet*_*dam 17 cookies jquery if-statement popup
我是jQuery的绝对新手.我正在学习,但有一条圣诞节信息,我需要立即启动并运行.
我把它们包含在页面的标题中:
<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>`
Run Code Online (Sandbox Code Playgroud)
然后使用jQuery弹出窗口跟随消息.这里是:
<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>
Run Code Online (Sandbox Code Playgroud)
在body我把信息中:
<div style="top: 199.5px; left: 200px; display: none;" id="dialog" class="window">
XMAS MESSAGE
<a href="#" class="close">Shut this popup.</a>
</div>
Run Code Online (Sandbox Code Playgroud)
到现在为止还挺好.下一步不是一遍又一遍地给我的回访者带来同样的信息(推迟六十天就足够了).
所以我想使用jQuery cookie插件设置一个cookie:
function setCookie() {
$.cookie('test_status', '1', { path: '/', expires: 60 });
return false;
}
Run Code Online (Sandbox Code Playgroud)
然后在下次访问者点击同一页面时找到该消息,并且在消息到期之前不显示圣诞消息.
现在if-else语句是我不熟悉的更高级的jQuery.那么,有人可以向我解释一下吗?
Sud*_*oti 14
这种东西可能会有所帮助:
$(document).ready(function(){
if ($.cookie('test_status') != '1') {
//show popup here
$.cookie('test_status', '1', { expires: 60}); }
});