how to check if fancybox exists on page already

后端 未结 5 1841
猫巷女王i
猫巷女王i 2021-02-09 01:46

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

5条回答
  •  礼貌的吻别
    2021-02-09 02:30

    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.

提交回复
热议问题