How to conditionally load jQuery plugin?

I have an asp.net UserControl that can be added several times to a page. In this UserControl, I use the jQuery plugin, which I would only like to download once. I use the following code snippet to conditionally load jQuery itself:

if (typeof jQuery == 'undefined') { // load jquery var script = document.createElement('script'); script.type = "text/javascript"; script.src = "/_layouts/SprocketValidator/jQuery.js"; document.getElementsByTagName('head')[0].appendChild(script); } else { //already loaded } 

Will there be an equivalent to check if the jQuery plugin is undefined? The plugin I use is a digital buffer input / output plugin .

+4
source share
2 answers

Try:

 if (typeof jQuery.fn.mask == 'undefined') 

jquery-methods (usually also with plugins) are stored as $.fn properties, so you need to check if the method available inside the plugin (e.g. mask() ) is available as a member of $.fn

+3
source

you can use something like YepNope.js , which you can find here here . And then you can write your javascript as follows:

 yepnope({ test: window.JSON, nope: "/_layouts/SprocketValidator/jQuery.js", complete: function () { var data = window.JSON.parse('{ "json" : "string" }'); } }); 
0
source

All Articles