javascript - 实际使用脚本异步或延迟等框架

标签 javascript html script-tag

一直在研究script标签中asyncdefer的使用。鉴于以下...

async - Allows the rest of the html to parse, while the JS script is being loaded. Once the script is completely loaded then the html parser is paused to execute the script. However, note that the order of loading can't be controlled.

defer - Also allows the rest of the html to parse, however the loaded js is only executed once the html parsing is complete.

So, if you can afford to show your html without loading your js script first... use async. If more than one script is involved and the order they are loaded matters then use defer. On the other hand, if your scripts MUST be loaded first (and can't be rewritten to allow the html to parse first) then just load the script without using async or defer in the header.

这如何适应像 AngularJS 这样的现代框架……因为延迟加载会导致错误。任何指针将不胜感激。

最佳答案

考虑到 Angular 应用程序完全依赖于 Angular JS 文件和依赖项,延迟或异步它们没有多大意义,除非你有后备(比如加载状态,也许?)。然而,话虽如此,为所有脚本添加 defer 可能会很好,但在性能方面作用不大。添加异步可能会破坏事情,因为某些依赖项需要在其他依赖项之前加载。

关于javascript - 实际使用脚本异步或延迟等框架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34915608/

相关文章:

html - '显示 : inline block' trick not working in firefox

jquery - 我需要一些帮助来居中和获取内联文本和图像 (Jquery)

Javascript dd/mm/yyyy 日期检查?

html - Content-Script-Type 和 Content-Style-Type 有什么意义

javascript - React setState 回调不会重新渲染快速切换的状态

javascript - 滚动经过某个div时如何使粘性导航改变颜色?

javascript - 在表的每一行中查找下拉菜单的选定值

javascript - 尝试在 React 组件中运行 Ko-Fi 小部件。在 DIV 中运行 <script> 命令

javascript - 正在运行的脚本标签会阻止其他脚本标签的下载吗?

javascript - 我们可以用 `name ` 设置属性而不是 `class` 或 `id`