how to check if fancybox exists on page already

≯℡__Kan透↙ 提交于 2019-12-09 05:42:48

问题


I am trying to check if a fancybox has been loaded already. If so then I run an ajax script to put content inside.

If not I open a fancybox iframe and it fetches content.

Here is the code:

if ($('div#fancybox-frame:empty').length >0) { 
    alert('it is empty');

$.fancybox({
   'width': '80%',
    'height': '80%',
    'autoScale': true,
    'transitionIn': 'fade',
    'transitionOut': 'fade',   
    'type': 'iframe', 
    'href': '/show_photo'
});
 }
 else{
    alert('it is full');   
    $.ajax({
        type: "GET",
        url: "/show_photo",
        success: function(data){
        cropping_arrived();
        }
    });
  }
});

It always returns that it is full even when I know there is not fancybox open.

Any ideas?


回答1:


This should work without testing for .length

if ($('div#fancybox-frame:empty'))

However, :empty does check for text nodes. So if you div has a line break, i.e.,

<div id="#fancybox-frame">
</div>

it might choke. In that case, place the opening and closing tags on the same line.




回答2:


I made it work with below code:

if(typeof $.fancybox == 'function') {
     fancy box loaded;
} else {
     fancy box not loaded;
}



回答3:


This works for me

if ($('#fancy_content:empty').length > 0)
{
    alert('Fancybox Open');
}
else
{
    alert('Fancybox NOT Open');
}



回答4:


I've been building an app using Fancybox, so I am posting my findings on different related threads I find discussing the issues I have encountered. Amit Sidhpura's solution is great to check if the Fancybox JS script is present in the dom or, in other words, if the $.fancybox plugin is present.

However, it does not tell you whether Fancybox has been initialized or not. In order to do that, you can do the following:

function fancyboxIsInit() {
    var fbInit = false;
    if( typeof $.each( $(document).data('events') !== 'undefined' ) {
        $.each( $(document).data('events').click, function(i, v) {
            if( v.namespace === 'fb-start' ) fbInit = true;
        });
    }
    return fbInit;
}

Or the following:

function fancyboxIsInit() {
    var fbInit = false;
    if (typeof $._data(document, 'events') !== 'undefined') {
        $.each($._data(document, 'events').click, function (i, v) {
            if (v.namespace === 'fb-start') fbInit = true;
        });
    }
    return fbInit;
}

Depending on which jQuery version you are running.

JSFiddle:

Check out this JsFiddle for reference.




回答5:


In my case for ending up here in this thread, I had a script checking if user input had been detected through mouse movement or keyboard activity. However the parent page were not detecting activity when being inside a fancybox iframe.

To adjust and compensate for this fact, I let the script running in the background know if there is an open iframe in the DOM (as I am not using iframes besides with fancybox) by using the following code:

if ($('body iframe').length > 0)
{
    console.log('Fancybox Open');
}
else
{
    console.log('Fancybox NOT Open');
}

You could also choose to have your iframe content have a specific ID or Class that you measure, however take in mind that when the HTML element is dynamically instigated you have to target an existing parent selector first which were present at DOM to be able to find your newly created element.



来源:https://stackoverflow.com/questions/6706145/how-to-check-if-fancybox-exists-on-page-already

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!