这是我的代码:
<script src='app.js' defer></script> //very big js file
//lots of html stuff
<script> alert(1);</script>
问题是,这些是异步加载的。有没有办法等待第二个脚本直到第一个脚本加载?
如果您使用 jQuery,有一个非常简单的方法可以通过获取脚本 https://api.jquery.com/jquery.getscript/功能。只需添加加载后需要执行的脚本部分并将其作为参数传递给函数即可。
$.getScript( "app.js" )
.done(function( script, textStatus ) {
console.log( textStatus );
//your remaining code
})
.fail(function( jqxhr, settings, exception ) {
//script fail warning if you want it
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)