gpt4 book ai didi

javascript - @async 脚本会在解析器遇到其标签之前运行吗?

转载 作者:行者123 更新时间:2023-11-28 02:32:50 25 4
gpt4 key购买 nike

这就是我的意思。举个例子:

<!doctype html>
<html>
<head><title>test page</title></head>
<body>
Will this always be the first thing on the screen every time?
<script>document.write('Will this be 2nd?');</script>
<script async src="http://foo.bar/baz.js"></script>
</body>
</html>

...其中 baz.js 包含以下内容:

var s = document.createElement('span');
s.innerHTML = 'Is there any possibility that this will be the first thing to appear on the screen whatsoever, like it\'s being loaded over a really badass fiber optic connection?';
document.body.appendChild(s);

我问这个问题是因为我的脚本依赖于页面上已经存在的 body 元素供我附加,但我无法控制我的标记插入页面上的位置,除了以下事实:我知道它会在 body 的某个地方。 是否有浏览器在开始主要一轮解析之前对 asyncdefer 脚本的标记进行扫描,以查看是否有任何内容应该提前开始下载?

如果问题不够清楚,请告诉我,我会尽力澄清。

谢谢

最佳答案

在解析器到达 script 元素之前,它们不会被执行。

您的“这是第二个吗?” 脚本将始终在另一个脚本之前运行。

在这两种情况下,您都可以依赖 document.body

More reading关于asyncdefer的完整细节。

关于javascript - @async 脚本会在解析器遇到其标签之前运行吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13873639/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com