0

Anybody familiar with patterns for:

  • Checking for existing jQuery extension most safely?
  • And how to exit from it safely if code can not be executed.

Like

(function($){
    if (jQuery.fn.pluginName)
    ......
    }
})(jQuery);

10x, BR

kidwon
  • 4,448
  • 5
  • 28
  • 45
  • 1
    Check this out - http://stackoverflow.com/questions/400916/how-can-i-check-if-a-jquery-plugin-is-loaded - basically - if(jQuery().pluginName) { //run plugin dependent code } – hyankov Dec 12 '12 at 21:28

1 Answers1

1

The $.fn namespace is populated with functions. If the plugin exists you can use this

if ($.fn.myPlugin instanceof Function) {
    // the plugin exists
} else {
    // the plugin does not exist
}

Also if that wasnt enough, you can also check using

if (typeof $.fn.myPlugin === 'function') {
    // The plugin exists
} else {
    // The plugin does not exist
}
David Barker
  • 14,484
  • 3
  • 48
  • 77