简体   繁体   中英

For what is defer=“defer” in js?

I'm messing around with Three.js. I discovered, that it only works in the following way:

<script src="script.js" defer="defer"></script>

But i don't know, why this defer="defer" is so important...

Can you help me?

Best regards Tobi P.

It causes the browser to defer parsing of the script (not necessarily delay loading).

Normally when the browser encounters a script tag, it pauses any further procesing of the HTML until the script has been downloaded, fetched, parsed (then compiled on most browsers) and executed. This is necessary to allow the javascript to inject html via document.write().

However this often means that the user is left looking at a blank screen for a long time.

With the defer tag, the script is not parsed/compiled until the HTML is fully loaded. If you have multiple script tags with the defer atrtribute, then the order in which they are parsed is maintained.

This is all extensively documented on the internet. The w3c pages are a good place to start (but can be a bit terse).

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