admin管理员组文章数量:1279209
I am dynamically inserting the jQuery library on a page via <script>
tag:
jq = document.createElement('script');
jq.setAttribute('src','//ajax.googleapis/ajax/libs/jquery/1.7.1/jquery.min.js');
b.appendChild(jq);
Then I have some jQuery script that needs to run after the jQuery library has finished loading and is ready for use:
$(f).load(function() {
$(f).fadein(1000);
});
How can I make it wait for jQuery to load?
I am dynamically inserting the jQuery library on a page via <script>
tag:
jq = document.createElement('script');
jq.setAttribute('src','//ajax.googleapis./ajax/libs/jquery/1.7.1/jquery.min.js');
b.appendChild(jq);
Then I have some jQuery script that needs to run after the jQuery library has finished loading and is ready for use:
$(f).load(function() {
$(f).fadein(1000);
});
How can I make it wait for jQuery to load?
Share Improve this question edited Jun 7, 2020 at 15:22 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Jan 14, 2012 at 18:30 Web_DesignerWeb_Designer 74.6k93 gold badges209 silver badges266 bronze badges 1- Rob's answer below is perfect, though let me shimmy on here that what you're doing is (a good) part of what Modernizr was made for. :D – Richard Neil Ilagan Commented Jan 14, 2012 at 19:25
1 Answer
Reset to default 14Specify an onload
event at the to-be-inserted script tag:
function onLoad() {
$(f).load(function() {
$(f).fadein(1000);
});
}
jq = document.createElement('script');
jq.onload = onLoad; // <-- The magic
jq.src = '//ajax.googleapis./ajax/libs/jquery/1.7.1/jquery.min.js';
b.appendChild(jq);
An alternative way, if you cannot control the script insertion code, you can use a poller:
(function() {
function onLoad() { ... } // Code to be run
if ('jQuery' in window) onLoad();
else {
var t = setInterval(function() { // Run poller
if ('jQuery' in window) {
onLoad();
clearInterval(t); // Stop poller
}
}, 50);
}
})();
本文标签:
版权声明:本文标题:javascript - Execute my jQuery script after dynamically inserted jQuery library has finished loading - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741269441a2369000.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论