3

i want to check if jquery plugins are already initialized / included (prevent dublicate resource loading) and if not i want to add the plugin by CDN or by a local source.

Anyone can help me with that Problem?


Update:

I'm currently building a custom template in drupal. In that template i'm using a slick slider for scrolling through pictures in a content gallery. I also want to use a custom module for a slideshow, which uses the slick slider too (js, css integrated in the module). So now i want to check if the slick slider of the slideshow module is already initialized, if not i want to load the slick slider js via cdn.

Hope that helps for understanding my problem.


Update 2: Resolved the problem by using php and drupal core functions. I also found a solution with the modernizr.js load function -> asynchronous loading.

Pavlo
  • 43,301
  • 14
  • 77
  • 113
themca
  • 323
  • 3
  • 15

3 Answers3

5

You can check and detect the plugin using this

if(jQuery().pluginName) {
    //run plugin
}
Fatkhan Fauzi
  • 362
  • 1
  • 11
3

You can try this:

if (!jQuery.fn.plugin) {
    jQuery.getScript('http://url/to/the/script');
}
jcubic
  • 61,973
  • 54
  • 229
  • 402
2

You can check for typeof jQuery. if undefined, load from CDN:

if (typeof jQuery == 'undefined') {
{
  //load from CDN  
}

or

if(!window.jQuery)
{
  //load from CDN  
}
Milind Anantwar
  • 81,290
  • 25
  • 94
  • 125