Shawn Crigger
It's A Digital World by Shawn Crigger

It's A Digital World by Shawn Crigger

How to check if plugin is loaded in jQuery

How to check if plugin is loaded in jQuery

Shawn Crigger's photo
Shawn Crigger
·May 15, 2011·

1 min read

This is a short simple quick tip, I re-use a lot of my code, ( it's called going green ) anyway sometimes I forget to remove some common jQuery plug-in call's that I use a lot like Lightboxes, Choosen Selects, jEditable, etc, etc. So I find just adding a quick check to make sure the method exists before calling it is very helpful.

One way of checking if a method exists is like this

  if (typeof $.fn.pluginname != 'undefined')

Or the short-hand version

  if($.fn.pluginname)

Just replace "pluginname" with whatever your calling like below

  if($.fn.pluginname)
    // Verify there is a method named "chosen", then verify we need to use it.
    if ($(".chzn-select").length > 0 && typeof $.fn.chosen !== 'undefined')
    {
        $(".chzn-select").chosen();
    }
 
Share this