admin管理员组文章数量:1310404
I'd like to pull in the jquery library from my javascript include.
Does this work 100% of the time? Is there a better way?
(function() {
var loadJquery = function (cb) {
var addLibs = function () {
if (typeof(document.body) == "undefined" || document.body === null) {
setTimeout(addLibs, 100);
return;
}
var node = document.createElement("script");
node.src = ".3.2.min.js";
document.body.appendChild(node);
checkLib();
};
var checkLib = function () {
if (typeof($) == "undefined" || $("*") === null) {
setTimeout(checkLib, 100);
return;
}
cb($.noConflict());
}
addLibs();
}
loadJquery(function($){
// Do stuff with $
$(document.body).css("background", "black");
});
})();
Change the node.src
and $.noConflict
to YUI
if you want YUI3, or YAHOO
if you're YUI2, etc.
I'd like to pull in the jquery library from my javascript include.
Does this work 100% of the time? Is there a better way?
(function() {
var loadJquery = function (cb) {
var addLibs = function () {
if (typeof(document.body) == "undefined" || document.body === null) {
setTimeout(addLibs, 100);
return;
}
var node = document.createElement("script");
node.src = "http://jqueryjs.googlecode./files/jquery-1.3.2.min.js";
document.body.appendChild(node);
checkLib();
};
var checkLib = function () {
if (typeof($) == "undefined" || $("*") === null) {
setTimeout(checkLib, 100);
return;
}
cb($.noConflict());
}
addLibs();
}
loadJquery(function($){
// Do stuff with $
$(document.body).css("background", "black");
});
})();
Change the node.src
and $.noConflict
to YUI
if you want YUI3, or YAHOO
if you're YUI2, etc.
4 Answers
Reset to default 4There's only one issue I can think of with this approach (and this has e up twice in questions on SO in the last month).
The issue is that jQuery as of 1.3.2 cannot detect if the document is loaded if it is itself included after the document has loaded (i.e. dynamic <script>
inclusion as you are doing, bookmarklet, etc) in non-IE browsers. In such cases the function passed to .ready()
will never be called.
Here is the bug report for the issue: http://dev.jquery./ticket/4196 Nothing has happened with it for the last 8 months.
Now, the way you have designed loadJquery
as I see it should effectively replace $(document).ready()
so you may not need to worry. But any code that is written in the $(document).ready()
function-passing style will not execute.
From posting on our internal mailing lists at work, I was pointed to this:
There are many cross-browser edge cases involved in dynamically injecting scripts. If rolling the YUI 3 seed file into your script isn't an option and if you can't depend on the property to already have the YUI 3 seed loaded, I'd remend using LazyLoad (shameless plug) to bootstrap your script: http://github./rgrove/lazyload/
You can merge lazyload.js into your script, then use something like this to load the YUI 3 seed if it's not already on the page:
(function () {
function init() {
YUI().use('node', function (Y) {
// ... do my stuff ...
});
}
if (YUI) {
init();
} else {
LazyLoad.js('http://yui.yahooapis./3.0.0/build/yui/yui-min.js',
init);
}
})();
If you're looking for a standard way, instead of ing up with your own implementation, you could perhaps consider:
- Google's google.load() for loading popular libraries
- The CommonJS SecurableModules - see Kris Kowal's Chiron for a browser implementation.
See my answer here for a reliable asynchronous require() function: How do I include a JavaScript file in another JavaScript file?
本文标签: jqueryStandard way to include javascript library from javascriptStack Overflow
版权声明:本文标题:jquery - Standard way to include javascript library from javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741784156a2397483.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论