Can you use both the async and defer attributes on a HTML script tag?
我想使用
由于Internet Explorer 5.5+支持
因此,我的问题是上述代码是否有效HTML?如果没有,是否可以在没有
来自规范:https://www.w3.org/tr/2011/wd-html5-20110525/scripting-1.html attr script async
The defer attribute may be specified even if the async attribute is specified, to cause legacy Web browsers that only support defer (and not async) to fall back to the defer behavior instead of the synchronous blocking behavior that is the default.
问题是,你希望它能做什么?如果同时存在async和defer,我希望脚本被延迟,并且只在浏览器空闲时在domcontentloaded之后执行,但是如果我正在正确地阅读规范,那么如果设置了async并且异步加载了脚本,则似乎会忽略defer,因此只要脚本可用,就会立即执行,这可能早于domcont加载并可能阻塞其他资源。
There are three possible modes that can be selected using these attributes. If the async attribute is present, then the script will be executed asynchronously, as soon as it is available. If the async attribute is not present but the defer attribute is present, then the script is executed when the page has finished parsing. If neither attribute is present, then the script is fetched and executed immediately, before the user agent continues parsing the page.
https://www.w3.org/tr/2011/wd-html5-20110525/scripting-1.html attr script async
是的,它是有效的HTML,可以像预期的那样工作。
任何与W3C兼容的浏览器都将识别async属性并正确处理脚本,而传统的IE版本将识别defer属性。
因为这两个属性都是布尔值,所以您不必分配任何值。
不幸的是,当指定
我个人认为,忽视
1 2 3 4 5 | <!-- we want"jQuery" ASAP and still in"defer" queue. But defer is ignored. --> <script src="jquery.min.js" async defer> <!-- this doesn't blocks the content and can wait the full page load, but requires"jQuery" --> <script src="some_jquery_plugin.min.js" defer> |
在此示例中,可以在加载jquery之前加载并执行"some-jquery-plugin.min.js",并且将失败。:(
因此,有两种方法可以解决这个问题:要么只使用