当前位置: 首页 > news >正文

【HTML】浅谈 script 标签的 defer 和 async

The async and defer attributes are boolean attributes that indicate how the script should be evaluated. There are several possible modes that can be selected using these attributes, depending on the script’s type.
async 和 defer 属性是布尔属性,它们指示脚本应该如何被评估。可以使用这些属性选择几种可能的模式,具体取决于脚本类型。

For external classic scripts, if the async attribute is present, then the classic script will be fetched in parallel to parsing and evaluated as soon as it is available (potentially before parsing completes). If the async attribute is not present but the defer attribute is present, then the classic script will be fetched in parallel and evaluated when the page has finished parsing. If neither attribute is present, then the script is fetched and evaluated immediately, blocking parsing until these are both complete.
对于外部经典脚本,如果存在 async 属性,则经典脚本将在解析的同时并行获取,并在可用时立即评估(可能在解析完成之前)。如果不存在 async 属性但存在 defer 属性,则经典脚本将并行获取,并在页面解析完成后评估。如果这两个属性都不存在,则脚本将立即获取并评估,阻塞解析直到这些操作都完成。

For module scripts, if the async attribute is present, then the module script and all its dependencies will be fetched in parallel to parsing, and the module script will be evaluated as soon as it is available (potentially before parsing completes). Otherwise, the module script and its dependencies will be fetched in parallel to parsing and evaluated when the page has finished parsing. (The defer attribute has no effect on module scripts.)
对于模块脚本,如果存在 async 属性,则模块脚本及其所有依赖项将在解析的同时并行获取,模块脚本将在可用时立即评估(可能在解析完成之前)。否则,模块脚本及其依赖项将并行获取,并在页面解析完成后评估。( defer 属性对模块脚本没有影响。)

This is all summarized in the following schematic diagram:
所有这些内容都总结在下方的示意图中:

在这里插入图片描述

The defer attribute may be specified even if the async attribute is specified, to cause legacy web browsers that only support defer (and not async) to fall back to the defer behavior instead of the blocking behavior that is the default.
即使指定了 async 属性,也可以指定 defer 属性,以使仅支持 defer (但不支持 async )的旧版网络浏览器回退到 defer 行为,而不是默认的阻止行为。

参考资料

HTML Standard. https://html.spec.whatwg.org/multipage/scripting.html#attr-script-async

http://www.dtcms.com/a/304970.html

相关文章:

  • JAVA中JDK8新特性(Stream流)
  • Unity打包后,在Windows7系统上无法播放VideoPlayer视频
  • Python 中数据的位运算和状态编码(掩码)详解
  • SeeMoE:从零开始实现一个MoE视觉语言模型
  • 项目质量如何把控?核心要点分析
  • Python入门第四课:高级特性与实战:应用列表推导式与生成器
  • Makefile 与 CMake 关系指南
  • 基于 xlsx-js-style 的 Excel 导出工具实现导出excel
  • JSON解析(day20)
  • 飞机大战小游戏
  • string
  • 如何进行项目复盘?核心要点分析
  • 从线下到线上:解析足浴按摩平台如何实现成本降低80%的技术方案
  • java基础面试题(5)
  • 高精度惯性导航IMU传感器价格供应商
  • 数据处理和统计分析——04 Pandas DataFrame
  • Python的‌魔法方法‌
  • Java学习第八十七部分——四次挥手
  • Java设计模式之《备忘录模式》
  • 80V降5V,输出电流4A,应用于车载充电器
  • 工厂方法模式 Factory Method Pattern
  • 监控场景视频质量异常修复:陌讯动态增强算法实战解析
  • 深入浅出设计模式——创建型模式之建造者模式 Builder
  • LangChain API 功能介绍和使用示例
  • 单卡10分钟部署MiniCPM4-0.5B:轻量级大模型本地运行指南
  • ESP8266 AT 固件
  • Linux信号量:进程同步与互斥的核心机制
  • 人形机器人指南(十四)集成
  • 248°视野里的温柔革命:当清洁成为治愈城市的艺术
  • Qt 移动应用界面设计原则