超人AI导航

超人AI导航 超人AI导航 超人AI导航 超人AI导航
  • 站点推荐
    • 技术文档
      • 后端开发
      • 前端开发
      • 移动开发
      • 人工智能
      • 编程语言
    • 文章博客
      • 游戏教程
      • 软件教程
    • 关于我们
  • AI写作工具
  • AI图像工具
    • 常用AI图像工具
    • AI图片插画生成
    • AI图片背景移除
    • AI图片无损放大
    • AI图片优化修复
    • AI图片物体抹除
  • AI视频工具
  • AI办公工具
    • AI幻灯片和演示
    • AI表格数据处理
    • AI文档工具
    • AI思维导图
    • AI会议工具
    • AI效率提升
  • AI对话聊天
  • AI音频工具
  • AI编程工具
  • AI设计工具
  • AI内容检测
  • AI学习网站
  • AI开发框架
  • AI提示指令
  • AI训练模型
  • AI模型评测
超人AI导航 超人AI导航
  • 超人AI导航
    • AI写作工具
    • AI图像工具
    • AI视频工具
    • AI办公工具
    • AI对话聊天
    • AI音频工具
    • AI编程工具
    • AI设计工具
    • AI内容检测
    • AI学习网站
    • AI开发框架
    • AI提示指令
    • AI训练模型
    • AI模型评测
  • 技术文档
    • 后端开发
    • 前端开发
    • 移动开发
    • 人工智能
    • 编程语言
  • 文章博客
    • 游戏教程
    • 软件教程
  • 关于我们
首页 •技术文档•前端开发 •在

我理解HTML中 <script async> 的概念:

  • 脚本以异步方式下载
  • 下载完成后就会立即执行
  • 它不会等待DOM解析完成

不过我对一件事有点困惑。有些人说,通过async加载的JS可能与DOM无关。

我想知道,实际应该怎么编写这个JS文件?它真的完全不能使用DOM吗?

我知道async的原理,但不知道其中的JS文件到底长成什么样。能否给出一个能够与 <script async> 配合良好的JS示例?

解决方案

有些人说,通过async加载的JS可能与DOM无关。

是的,但要强调“可能”这一点,因为 async 脚本 可以 使用DOM,它只是需要一些初始入口点逻辑来确保安全,因为脚本的第一行 可能 在任何 <body> HTML/DOM可用之前就会执行——或者它 可能 在DOM加载的任意时刻运行,因此由于无法保证浏览器文档的DOM状态,这意味着脚本需要以 防御性 编写。

所以放在一个 async <script> 元素中的JS 可以 使用DOM,但 脚本必须先检查 document.readyState 首先——并且根据当前的 readyState,要么立即继续执行——要么必须安排自己在 DOMContentLoaded 发生时或之后再运行。

例如:

MyAsyncLoadedScript.js:

// (This is a plain ol' JS, with top-level code. No modules, no UMD, no AMD, no require(), no imports, etc).

if( document.readyState === 'loading' ) {

    // Document is not ready yet (i.e. it is *not* yet safe to manipulate the DOM, excepting objects like `document` and `window`).
    // ...so we cannot safely run this script's main function yet.
    // ...so schedule it to run on DOMContentLoaded using addEventListener:
    document.addEventListener( 'DOMContentLoaded', theRestOfTheScriptProgram );

    // However, if this script depends on all external resources (images, other scripts, etc) being fully loaded before it runs, then use the `window` 'load' event instead of 'DOMContentLoaded'.
    // See https://developer.mozilla.org/en-US/docs/Web/API/Window/load_event
//  window.addEventListener( 'load', theRestOfTheScriptProgram );
}
else if( document.readyState === 'complete' ) {

    // The document (the DOM and all resources) is fully loaded already, i.e. the 'load' event already fired.
    // ...so it's safe to immediately run the main function synchronously:
    theRestOfTheScriptProgram();
}
else /* implicit: document.readyState === 'interactive' */ {

    // The 'interactive' state sits in-between 'loading' and 'complete', from 'DOMContentLoaded' until 'load'.
    // ...so if the script does not depend on all external resources (images, videos, etc) being already-loaded, then it's safe to synchronously run the main function here:
    theRestOfTheScriptProgram();
}

function theRestOfTheScriptProgram() {

    // Inside this function, it's safe to manipulate the DOM.
    // For example, `document.querySelectorAll` will behave predictably.
}
JavaScript
站内所有文章版权归属LeftHeroAI导航站,无授权禁止任何主体转载、抄袭、复制内容,亦不得私自架设镜像站点。一经侵权,本站将通过法律途径追责。
上一篇

在JavaScript中,原始类型和引用类型存放在哪里?

下一篇

在Tailwind中创建基础颜色工具类

相关文章

如何通过Python/DBus在 KDE Plasma 6(Wayland)中获取活动窗口标题?
2026-07-12
3
在不使用CSS的情况下,如何在p5.js v2中控制可变字体的宽度轴?
2026-07-12
1
如何在Node.js中运行导入了使用其他模块解析方式转译的模块的JavaScript代码?
2026-07-12
1
Mongoose与 TypeScript
2026-07-12
5
InvalidPartitionsException: 由于文件描述符限制,无法增加分区数量
2026-07-12
1
Bash递归中数组里出现幽灵项的错误?
2026-07-12
2

最新文章

为什么在对同一个getter调用两次时,三元运算符的行为会不同?
2026-07-12
4
如何在地图上绘制带颜色的点位并添加图例?
2026-07-12
4
如何为嵌套的条带上色?
2026-07-12
4
我能创建一个用来反转改动的提交分支吗?
2026-07-12
3
将写入该文件的数据刷新到磁盘,以便其他程序读取
2026-07-12
4

热门工具

Lamini
Lamini
Stockimg AI
Stockimg AI
MMLU
MMLU
字语智能
字语智能
C-Eval
C-Eval
ProWritingAid
ProWritingAid
超人AI导航 超人AI导航
汇集全球数千款 AI 工具,每日持续更新,配套 AI 开发学习资源,轻松用 AI 提升工作效率。快捷键 Ctrl+D/⌘+D 一键收藏本站。

超人AI导航关于我们免责声明

Copyright © 超人AI导航  辽ICP备2024029316号-4