脚本异步标记是否保留脚本执行顺序?

编程入门 行业动态 更新时间:2024-10-11 07:31:25
本文介绍了脚本异步标记是否保留脚本执行顺序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

更多推荐

脚本异步标记是否保留脚本执行顺序?

本文发布于:2023-11-23 13:26:47,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1621608.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:脚本   标记   顺序

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!