Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to check if fancybox exists on page already

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?

like image 514
chell Avatar asked Jul 15 '11 11:07

chell


People also ask

How do I know if fancybox is loaded?

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

How do I open Fancybox on button click?

on('click', function() { $. fancybox(); }); });


3 Answers

I made it work with below code:

if(typeof $.fancybox == 'function') {
     fancy box loaded;
} else {
     fancy box not loaded;
}
like image 66
Amit Sidhpura Avatar answered Sep 30 '22 06:09

Amit Sidhpura


This works for me

if ($('#fancy_content:empty').length > 0)
{
    alert('Fancybox Open');
}
else
{
    alert('Fancybox NOT Open');
}
like image 27
Chan Avatar answered Sep 30 '22 08:09

Chan


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.

like image 30
Jason Gennaro Avatar answered Sep 30 '22 08:09

Jason Gennaro