Javascript 导入的节点不是作为异步添加到DOM的吗
我记得在某个地方读到,如果使用Javascript 导入的节点不是作为异步添加到DOM的吗,javascript,html,web-component,html-imports,Javascript,Html,Web Component,Html Imports,我记得在某个地方读到,如果使用appendChild将script标记添加到DOM中,则它不会阻塞,并且其行为就像具有async属性一样。今天我正在阅读,它有以下代码片段: var link = document.createElement('link'); link.rel = 'import'; link.href = 'file.html'; //link.setAttribute('async', ''); // make it async! 所以我想知道scripttag的行为与li
appendChild
将script
标记添加到DOM
中,则它不会阻塞,并且其行为就像具有async
属性一样。今天我正在阅读,它有以下代码片段:
var link = document.createElement('link');
link.rel = 'import';
link.href = 'file.html';
//link.setAttribute('async', ''); // make it async!
所以我想知道
script
tag的行为与link
tag的行为不一样吗?为什么手动添加async
属性?根据定义,每个首先,rel=“import”
的行为仍然是一个工作草稿,作者链接到的是编辑草稿。这意味着所有这些都可能在明天改变。因此,几乎没有理由认为这些行为不会结合在一起。当然,也没有理由认为他们会。。。其次,如前所述,不同的浏览器/版本以不同的方式处理插入的脚本script
元素。@MikeMcCaughan,谢谢你的评论。关于script
标记,据我所知,所有现代浏览器都会将脚本注入脚本的async
默认为true
。
document.head.appendChild( link )
//link should be async if it uses the element below:
document.body.appendChild( element )