简体   繁体   中英

Why can you only use defer when src is specified?

When validating code that uses the <script> tag I get the following warning on validator.w3.org

Element script must not have attribute defer unless attribute src is also specified.

I don't understand the need for this, could someone explain it?

The point of defer is to say "You can keep on parsing the HTML before waiting for the script to arrive".

If the script is right there in the HTML, then you can't (since the script is in the way in the source code) and it would be pointless (since there is no external dependancy that would block parsing).

Defer attribute means asyncronous script loading (by default scripts are loaded by browser in first turn, and are executed immedialtely after loading completed).

Without src property there will be nothing to load, and nothing to defer.

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