只显示一次弹出窗口(夸张弹出窗口)

时间:2017-03-21 21:50:28

标签: javascript jquery cookies popup

每次访问时只尝试显示此弹出窗口。我觉得我错过了什么

<script src="http://code.jquery.com/jquery-1.7.min.js">
<link href="http://cdnjs.cloudflare.com/ajax/libs/magnific-popup.js/1.1.0/magnific-popup.css" rel="stylesheet" type="text/css" media="all"/>
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/magnific-popup.js/1.1.0/jquery.magnific-popup.min.js"></script>
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.4.0/jquery.cookie.js"></script>

<script>
$(document).ready(function() {
var check_cookie = $.cookie('bsw');
   if(check_cookie == null){
    $.cookie('bsw', 'yes', {expires: 1 });
  $('.image-link').magnificPopup({type:'image'});
});
</script>

<script>
(function($) {
    $(window).load(function () {
        // retrieved this line of code from http://dimsemenov.com/plugins/magnific-popup/documentation.html#api
        $.magnificPopup.open({
            items: {
                src: '/bsw-10-off.png'
            },
            type: 'image'

          // You may add options here, they're exactly the same as for $.fn.magnificPopup call
          // Note that some settings that rely on click event (like disableOn or midClick) will not work here
        }, 0);
    });
})(jQuery);
</script>

我没有关闭或打开正确的东西吗?我觉得这个问题在文档就绪函数中,但我可能错了。任何帮助,将不胜感激。谢谢。

以下是没有cookie代码的代码

<link href="http://cdnjs.cloudflare.com/ajax/libs/magnific-popup.js/1.1.0/magnific-popup.css" rel="stylesheet" type="text/css" media="all"/>
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/magnific-popup.js/1.1.0/jquery.magnific-popup.min.js"></script>

<script>
$(document).ready(function() {
  $('.image-link').magnificPopup({type:'image'});
});
</script>

<script>
(function($) {
    $(window).load(function () {
        // retrieved this line of code from http://dimsemenov.com/plugins/magnific-popup/documentation.html#api
        $.magnificPopup.open({
            items: {
                src: 'http://www.barbershopwindow.com/bfimages/bsw-10-off.png'
            },
            type: 'image'

          // You may add options here, they're exactly the same as for $.fn.magnificPopup call
          // Note that some settings that rely on click event (like disableOn or midClick) will not work here
        }, 0);
    });
})(jQuery);
</script>

1 个答案:

答案 0 :(得分:0)

而不是:

  

//从http://dimsemenov.com/plugins/magnific-popup/documentation.html#api

中检索到这行代码

初始化magnificPopup后,您可能只是触发点击事件。

此外, $ .cookie('bsw') 会返回undefined,如果Cookie不存在,则返回null。

工作jsfiddle

$(document).ready(function () {
    var check_cookie = $.cookie('bsw');
    if (check_cookie == undefined) {
        $.cookie('bsw', 'yes', {expires: 1});
        $('.image-link').magnificPopup({type: 'image'}).trigger('click');
    }
});
<script src="https://code.jquery.com/jquery-1.12.3.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/magnific-popup.js/1.1.0/magnific-popup.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/magnific-popup.js/1.1.0/jquery.magnific-popup.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.4.0/jquery.cookie.js"></script>



<a class="image-link" href="http://farm4.staticflickr.com/3721/9207329484_ba28755ec4_o.jpg">Open popup</a>