简体   繁体   中英

How does it matter whether we stick the `<script>` tag in the head or not, assuming we’re already using `async` or `defer`?

How does it matter whether we stick the <script> tag in the head or not, assuming we're already using async or defer ?

The script tag in question is for analytics. Apparently putting script tags anywhere else is off-spec , but I don't care.

As youve already pointed out, script tags belong to the header. The only reason to not do so was that scripts were loaded before the html was processed, causing a long waiting rendering. Thats why many people ignored the specs and embedded the scripts at the bottom. As this is not really a good way, the people writing the specs introduced async and defer . So no theres no reason to put asyncly loaded code into the body...

Sidenote: async and defer are not supported by all browsers yet. So may still use the old ( illegal ;) ) workaround...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM