Handle "is not a function" error in jquery

845 views Asked by At

I like to display a custom message to user when jquery plugin required is not loaded on page.

For example to use jshowoff plugin (http://ekallevig.com/jshowoff/) its mandatory to include jquery.jshowoff.min.js However I would like to put some code to initially check if jquery.jshowff.min.js is already loaded on the page or not. And if not loaded, display error message saying that required plugin is not available rather than error is browser console as $.jshowoff is not a function.

Could someone advice on how to implement this please ?

Thank you very much for your help.

1

There are 1 answers

1
T.J. Crowder On BEST ANSWER

If you use the plugin via a jQuery object, e.g.:

$(/*...stuff here...*/).somePlugin();

...then you can test whether it's loaded like this:

if (!$.fn.somePlugin) {
    // somePlugin is not loaded for some reason
}

If you use it directly from jQuery ($), e.g:

$.somePlugin();

...then you can test for that like this:

if (!$.somePlugin) {
    // somePlugin is not loaded for some reason
}