当前位置: 首页 > 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.xdnf.cn/news/1208971.html

相关文章:

  • 企业高性能web服务器
  • EnergyMath芯详科技 EMS4100/MES4000/MES3900
  • 如何保证DoIP的网络安全?
  • 基于 xlsx-js-style 的 Excel 导出工具实现导出excel
  • 40+个常用的Linux指令——下
  • haproxy应用详解
  • 从github同步新项目的两次挫折-2025.7.29
  • 【WRF工具】服务器中安装编译GrADS
  • 信创国产Linux操作系统汇总:从桌面到服务器,百花齐放
  • 【Golang】Go语言Map数据类型
  • 随缘玩 一: 代理模式
  • 计算器4.0:新增页签功能梳理页面,通过IO流实现在用户本地存储数据
  • MySQL数据库 mysql常用命令
  • 再谈亚马逊云科技(AWS)上海AI研究院7月22日关闭事件
  • 实现视频实时马赛克
  • P1098 [NOIP 2007 提高组] 字符串的展开
  • python案例:基于python 神经网络cnn和LDA主题分析的旅游景点满意度分析
  • 小程序中事件对象的属性与方法
  • 微算法科技(NASDAQ:MLGO)应用区块链联邦学习(BlockFL)架构,实现数据的安全传输
  • Django自带的加密算法
  • 3D游戏引擎的“眼睛“:相机系统深度揭秘与技术实现
  • 14、distance_object_model_3d算子
  • 如何用命令行快速提取PPT中的所有图片?
  • 线程崩溃是否导致进程崩溃
  • 【嵌入式电机控制#18】有刷直流串级控制
  • MySQL图解索引篇
  • 大模型技术对部分岗位的影响
  • Apache Ignite 的分布式原子类型(Atomic Types)
  • 在CSS中,如果你想设置一个元素的高度(height)与其宽度(width)相匹配,但又希望宽度使用百分比来定义,你可以通过几种方式来实现。
  • 试用SAP BTP 02C:试用SAP HANA Schemas HDI Containers