除了组合,缩小,gzipping和缓存之外的所有问题。将脚本标记为异步仍然会保留脚本执行顺序吗?
All concerns of combination, minification, gzipping, and cache aside. Does marking a script as async still preserve script execution order?
假设我有一个页面,顶部包含jquery,后面是其他脚本,假设存在jquery。
Say I have a page where jquery is included at the top followed by other scripts at the bottom that assume the presence of jquery.
如果我用异步属性标记jquery脚本,我知道浏览器将继续解析我的html。但是,浏览器是否会保证在我的其他脚本执行之前加载jquery,或者可能无序发生导致未定义的 $ ?
If I mark the jquery script with the async attribute I know the browser will continue parsing my html. However, will the browser guarantee that jquery is loaded before my other scripts execute or might things happen out of order resulting in an undefined $?
推荐答案使脚本异步不保证任何同步执行,除了脚本本身。
Making a script async does not guarantee any synchronous execution, except that of the script itself.
<script async src="jquery.js"></script> <script> $.noop() // will definitely throw an error </script> <script async src="jquery.js"></script> <script src="someOtherNonAsyncScript.js"></script> <script> $.noop() // may or may not throw an error </script>更多推荐
脚本异步标记是否保留脚本执行顺序?
发布评论