How to check jQuery plugin and functions exists? How to check jQuery plugin and functions exists? javascript javascript

How to check jQuery plugin and functions exists?


if ($.fn.marquee) {    // there is some jquery plugin named 'marquee' on your page}


You can also do this. Let me take jQuery marquee example.

This is good if you are using only jQuery.

if($().marquee) {    // marquee is loaded and available}

OR

if($.fn.marquee !== undefined) {    // marquee is loaded and available}

Similar to above but Safe when you are using other JS frameworks Mootools etc.

if(jQuery().marquee) {    // marquee is loaded and available}

OR

if(jQuery.fn.marquee !== undefined) {    // marquee is loaded and available}


Slightly better:

if ($.isFunction($.fn.marquee)) {    // ...}

Maybe a little overkill, but this will ensure that it's at least a function.

Update January 2021:

Since jQuery.isFunction() has been deprecated as of version 3.3, the easiest & recommended way to check this is now:

if (typeof $.fn.marquee === "function") {    // ...}

Note that this may not be enough for certain very old browsers—see the $.isFunction() implementation for details.