I noticed the starter webpage project on Glitch had this in the
I was surprised to see a
<script> tag in the
<head> section, since I thought the best practice was to put these before
</body>. That couldn't be right...the Glitch devs seem to care about things like this. Then I saw the
MDN says about
This Boolean attribute is set to indicate to a browser that the script is meant to be executed after the document has been parsed, but before firing DOMContentLoaded.
Flavio Copes has a nice writeup on defer and async. My key takeaways:
deferonly works in the
deferthe browser fetches the script in parallel with parsing the HTML. Even if the browser finishes fetching the script, it doesn't run it until it's done HTML parsing.
asyncbecause parsing ideally should complete before script execution.
- The best practice is what Glitch does, shown above. Put
<script src="..." defer>in
Do you agree with this best practice?