admin管理员组文章数量:1023112
If scripts are loaded synchronously (incl. jQuery), then startup code I use is the following:
<script>
$(window).load(function () {
// some startup code
});
</script>
Now scripts are deferred:
<script defer type="text/javascript" src="//cdnjs.cloudflare/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<script defer type="text/javascript" src="...
How do I rewrite my startup code so it executes after deferred scripts are loaded and processed?
If scripts are loaded synchronously (incl. jQuery), then startup code I use is the following:
<script>
$(window).load(function () {
// some startup code
});
</script>
Now scripts are deferred:
<script defer type="text/javascript" src="//cdnjs.cloudflare./ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<script defer type="text/javascript" src="...
How do I rewrite my startup code so it executes after deferred scripts are loaded and processed?
Share Improve this question asked Jan 27, 2020 at 18:54 Denis KulaginDenis Kulagin 8,98719 gold badges71 silver badges137 bronze badges2 Answers
Reset to default 6The approach I've used is to listen to the 'load' event of that specific script tag - once it's done and good to go, the event will fire.
theScript.addEventListener('load', function () {
console.log(jQuery.fn.jquery);
});
<script defer id='theScript' type="text/javascript" src="//cdnjs.cloudflare./ajax/libs/jquery/2.1.4/jquery.min.js"></script>
this of course is easier by simply adding an onload to the tag
<script defer src="foo.js" onload="callback()"></script>
Your callback can trigger an event. Theoretically you can use the DOMContentLoaded event but I've had some trouble with js not being fully available when this fires
If scripts are loaded synchronously (incl. jQuery), then startup code I use is the following:
<script>
$(window).load(function () {
// some startup code
});
</script>
Now scripts are deferred:
<script defer type="text/javascript" src="//cdnjs.cloudflare/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<script defer type="text/javascript" src="...
How do I rewrite my startup code so it executes after deferred scripts are loaded and processed?
If scripts are loaded synchronously (incl. jQuery), then startup code I use is the following:
<script>
$(window).load(function () {
// some startup code
});
</script>
Now scripts are deferred:
<script defer type="text/javascript" src="//cdnjs.cloudflare./ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<script defer type="text/javascript" src="...
How do I rewrite my startup code so it executes after deferred scripts are loaded and processed?
Share Improve this question asked Jan 27, 2020 at 18:54 Denis KulaginDenis Kulagin 8,98719 gold badges71 silver badges137 bronze badges2 Answers
Reset to default 6The approach I've used is to listen to the 'load' event of that specific script tag - once it's done and good to go, the event will fire.
theScript.addEventListener('load', function () {
console.log(jQuery.fn.jquery);
});
<script defer id='theScript' type="text/javascript" src="//cdnjs.cloudflare./ajax/libs/jquery/2.1.4/jquery.min.js"></script>
this of course is easier by simply adding an onload to the tag
<script defer src="foo.js" onload="callback()"></script>
Your callback can trigger an event. Theoretically you can use the DOMContentLoaded event but I've had some trouble with js not being fully available when this fires
本文标签: javascriptHow to execute code after quotdeferredquot script is loaded and executedStack Overflow
版权声明:本文标题:javascript - How to execute code after "deferred" script is loaded and executed? - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745505661a2153592.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论