简体   繁体   English

在页面加载时启动Fancybox,而无需单击一次

[英]Launch Fancybox on page load without clicking only once

I want to launch a Fancybox 2 element when the page loads, but to only load once. 我想在页面加载时启动Fancybox 2元素,但只能加载一次。 I want to add a session cookie for at least 50 days. 我想添加一个会话cookie至少50天。

So far I've got the Fancybox element loading when the page loads: 到目前为止,页面加载时我已经加载了Fancybox元素:

window.jQuery(document).ready(function() {
    $.fancybox.open('#popup_box');
});

But I can't figure out how to implement it within a cookie if/else statement: 但是我不知道如何在cookie if / else语句中实现它:

$(document).ready(function () {
    // set cookie
    $.cookie('firstvisit', 1, {expires: 50});

    if ($.cookie('firstvisit') === 1) {
        //Do stuff if cookie doesn't exist like set a cookie with a value of 1
        $('#popup_box').hide();
    } else {
        $('#popup_box').show();
    }

});

I don't want the Fancybox 2 element to display if the user has visited the page 我不希望用户访问页面时显示Fancybox 2元素

First you need to check for cookie is set or not in browser. 首先,您需要检查浏览器中是否设置了cookie。 if not then set cookie and open pop up. 如果没有,则设置cookie并打开弹出窗口。 if yes then pop up will not open. 如果是,则弹出窗口将不会打开。

$(document).ready(function () {
   // set cookie
   if(!$.cookie('firstvisit')){
     $.cookie('firstvisit', 1, {expires: 50});
     $.fancybox.open('#popup_box');
   }    
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM