脚本异步标记是否保留脚本执行顺序? [英] Does script async tag preserve script order of execution?

查看:135
本文介绍了脚本异步标记是否保留脚本执行顺序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

除了组合,缩小,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>

这篇关于脚本异步标记是否保留脚本执行顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆