How to know if a plugin is active

Asked

Viewed 109 times

1

Personal am wanting to make my site only with a JS file and in it I would like to know if a certain plugin is loaded since for example the carousel I only load on a page.

How I test if the plugin is loaded to avoid giving errors in the console?

1 answer

2

You can create a recursive function that checks whether certain globes are broken. If they are not, it is called itself with a waiting time.

An example would be like this:

function ready(cb) {
    if (typeof window.meuPlugin == 'undefined') return setTimeout(ready.bind(null, cb), 200);
    else cb();
}

ready(function() { // esta callback será corrida quando a global estiver defenida
    alert('Tudo está carregado!');
});
<script>
    setTimeout(function() {
        // para simular o carregamento
        window.meuPlugin = function() {};
    }, 2000)
</script>

  • I liked it! That way I could check if the bootstrap and Angular, for example, were loaded @Sergio?

  • 1

    @Lucascosta yes, but for more advanced things you should have one Loader more decent than this example :) Require.js, for example. You can take a look here, a similar problem: http://answall.com/a/92772/129

  • @Sergio didn’t work. I tested with the plugin Simpleajaxuploader and only gets Undefined. I did like this: typeof window.Simpleupload == 'Undefined' ? console.log('not loaded') : console.log('loaded'); whether or not I load the plugin. Ex. <script src="{{ Asset('vendor/simple-ajax-Uploader/Simpleajaxuploader.js') }}"></script>

  • @Joaonivaldo can make an example in jsFiddle and put here?

  • @Sergio look I’ll quote a simple example. Let’s assume I have this line in my JS file, $("a#single_image"). fancyboxPlus(); and let’s say I don’t load facyboxPlus before this JS file. If you look at the Chrome console you will get an undeclared fancyboxPlus error. Here’s my question as I test if fancyboxPlus is loaded?

  • @Joaonivaldo tests with var carregado = !!$.fn.fancyboxPlus;

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.