Javascript 在解析器遇到@async脚本的标记之前,@async脚本是否会运行?
这就是我的意思。以以下为例:Javascript 在解析器遇到@async脚本的标记之前,@async脚本是否会运行?,javascript,html,performance,asynchronous,Javascript,Html,Performance,Asynchronous,这就是我的意思。以以下为例: <!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?')
<!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>
我这样做是因为我的脚本依赖于页面上已经存在的一个主体
元素,我可以附加到该元素,但我无法控制在页面上插入标记的位置,除非我知道标记将位于主体的某个位置是否有浏览器在开始其主要的一轮解析之前对标记进行扫描,以查找async
和defer
脚本,查看是否有任何内容需要提前开始下载?
如果问题不够清楚,请告诉我,我会尽力澄清
谢谢在解析器到达
脚本元素之前,不会对这些元素执行操作
您的“这是第二个吗?”
脚本将始终在另一个脚本之前运行
在这两种情况下,您都可以依赖document.body
在async
和defer
的全部细节上,直到解析器到达脚本
元素,才会对它们进行操作
您的“这是第二个吗?”
脚本将始终在另一个脚本之前运行
在这两种情况下,您都可以依赖document.body
有关async
和defer
的全部详细信息
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);