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
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.
Check out this JsFiddle for reference.