开发者

How to conditionally load a jQuery plugin?

I have an asp.net UserControl that can be added multiple times to a page. On this UserControl I am using a jQuery plugin that I would only like to load once. I am using the following piece of code 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
}

Would there be an equivalent for checking if a jQuery plugin is undef开发者_如何学Cined? The plugin I am using is the digitalBush masked input plugin.


Try:

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

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


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

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜