ReynierPM
ReynierPM

Reputation: 18660

Check if library is loaded or initialized

I'm using RS Slider JS library but I load it on a few pages not all the site then I have a script, which is common to all the site, where I trigger the libraries, for example:

var revapi;

jQuery(document).ready(function() {

       revapi = jQuery('.tp-banner').revolution(
        {
            delay:9000,
            startwidth:1170,
            startheight:500,
            hideThumbs:10,
            lazyLoad:"on"

        });

});

In some cases, when library JS aren't loaded because I don't need it then this part of the code triggers some "minors" errors, so how do I check if JS was loaded or if revolution() object exists or something else to avoid that problem? What do yours handle this?

Upvotes: 0

Views: 1439

Answers (2)

Dagg Nabbit
Dagg Nabbit

Reputation: 76736

So you're asking how to determine if an object returned by jQuery() has that revolution function?

Why not do something like this:

jQuery(document).ready(function() {

    var banner = jQuery('.tp-banner'),
        revapi;

    if (banner.revolution) {
        revapi = banner.revolution({
            delay:9000,
            startwidth:1170,
            startheight:500,
            hideThumbs:10,
            lazyLoad:"on"
        });
        // do other stuff with revapi
    }
});

Upvotes: 1

charlietfl
charlietfl

Reputation: 171669

You can check that a specific class exists before initializing your plugin. If it doesn't the plugin function never gets called and won't throw errors

if( $('.revolutionClass').length ){
   /* initialize plugin */

}

Upvotes: 1

Related Questions