admin管理员组文章数量:1414852
I thought I knew how to use 'defer' attribute when referencing external scripts from my HTML pages. I even thought there is no reason for me NOT to use it. But after a couple of unexpected things I started to research (even here) and I think I'm not 100% sure when it's safe to use it every time I use the script tag.
Is there somewhere a list of known use cases when defer should NOT be used?
I thought I knew how to use 'defer' attribute when referencing external scripts from my HTML pages. I even thought there is no reason for me NOT to use it. But after a couple of unexpected things I started to research (even here) and I think I'm not 100% sure when it's safe to use it every time I use the script tag.
Is there somewhere a list of known use cases when defer should NOT be used?
Share Improve this question edited Feb 23, 2022 at 22:41 Zoe - Save the data dump 28.3k22 gold badges128 silver badges160 bronze badges asked Sep 28, 2020 at 22:51 AlexAlex 91610 silver badges20 bronze badges2 Answers
Reset to default 5The only thing defer
does is run your script when the DOM has finished parsing, but before the DOMContentReady
event is fired off.
So: if your code does not depend on the DOM (directly, or indirectly through access to document
properties that can only be determined once the DOM is done), there is no reason to defer
. For example: a utility library that adds a new namespace ComplexNumbers
, with a ComplexNumber
object type and associated utility functions for performing plex number maths, has no reason to wait for a DOM: it doesn't need to be deferred. Same for a custom websocket library: even if your own use of that library requires performing DOM updates, it does not depend on the DOM and doesn't need defer
.
But for any code that tries to access anything related to the DOM: you need to use defer
. And yes: you should pretty much have defer
on any script that loads as part of the initial page load, and if you did your job right, none of those scripts interfere with each other when they try to touch the various pieces of the DOM they need to work with.
In fact, you should have both defer
*and* async
, so as not to block the page thread. The exception being if you're loading a type="module"
script, in which case you don't get a choice in deferral: it's deferred by default. but it'll still need async
.
DOMContentReady doesn't exist.
I added defer attribute to some external JS
And then I put my code inside
document.addEventListener('DOMContentLoaded', function() {
})
And I get a reference invalid when I try to use methods from the external JS.
https://developer.mozilla/en-US/docs/Web/API/Document/DOMContentLoaded_event
The DOMContentLoaded event fires when the HTML document has been pletely parsed, and all deferred scripts ( and ) have downloaded and executed. It doesn't wait for other things like images, subframes, and async scripts to finish loading.
But I'm not convinced.
本文标签: javascriptWhen NOT to use defer attributeStack Overflow
版权声明:本文标题:javascript - When NOT to use defer attribute - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745199680a2647310.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论