我记得在某处读到,如果使用 appendChild
将 script
标记添加到 DOM
,它不会阻塞,并且表现得好像它有 async
属性。今天我在看this article它有以下代码片段:
var link = document.createElement('link');
link.rel = 'import';
link.href = 'file.html';
//link.setAttribute('async', ''); // make it async!
所以我想知道 script
标签所描述的行为与 link
标签所描述的行为是否相同?为什么要手动添加 async
属性?
最佳答案
定义in the specification , 每个 <link rel="import>
标签未标记为 async
正在阻止解析器。
如果添加 <link>
与 appendChild()
,它不会阻止当前脚本的执行,但实际上它会阻止解析,直到加载导入的文档。
如果在那之后你添加另一个标签 -with appendChild()
- 导入文档引用了谁,您仍然需要指定 async
属性。
document.head.appendChild( link )
//link should be async if it uses the element below:
document.body.appendChild( element )
关于javascript - 导入节点未作为异步添加到 DOM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40894590/