我正在构建一个大型 jQuery 小部件,并且我有一些脚本依赖于运行这个小部件。当然,我可以将它们包含在我的 html 页面中,但这并不那么热门。我需要从我的小部件中加载它们。
我知道 $.getScript 但不幸的是,它似乎会在结束 } 之后运行代码,然后再运行其中的内容。它也不会缓存那些会降低站点速度的脚本。
我已经尝试过 LazyLoad 插件,与 $.getScript() 相同,它会在函数之后运行代码,然后再运行其中的代码。
我想做的是在 _create 函数中我可以调用 this._dependencies() 并加载我需要的所有必需的脚本,然后在 _create 中继续。
这是一些代码:
;(function ($, window, document, undefined) {
$.widget("test", {
create: function () {
this._dependencies();
$..setLocale(this.locale);
},
_dependencies: functio() {
LazyLoad.js(['jquery.i18n.js', 'test.i18n.js'], function () {});
}
});
})(jQuery, window, document);