preface
How to optimize web page loading speed?
Place the css file at the top of the page and the js file at the bottom of the page.
<! DOCTYPE html >
< html lang =" zh " >
< head >
< title > Hi </ title >
< script >
console . log ( "Howdy ~" ) ;
</ script >
< script src =" https://unpkg.com/vue @3.2.41/dist/vue.global.js " > </ script >
< script src =" https://unpkg.com/vue-router @4.1.5/dist/vue-router.global.js " > </ script >
</ head >
< body > Hello 👋🏻 ~ </ body >
</ html >
-
Print on the console: Howdy ~ -
Request and execute vue.global.js -
Request and execute vue-router.global.js -
Show on the page: Hello 👋🏻 ~ -
Trigger DOMContentLoaded Events
<script defer>
This Boolean property is set to notify the browser that the script will be executed after the document is parsed and before the DOMContentLoaded event is triggered.
The script with the defer attribute will block the DOMContentLoaded event until the script is loaded and parsing is completed.
<! DOCTYPE html >
< html lang =" zh " >
< head >
< title > Hi </ title >
< script >
console . log ( "Howdy ~" ) ;
</ script >
< script defer src =" https://unpkg.com/vue @3.2.41/dist/vue.global.js " > </ script >
< script defer src =" https://unpkg.com/vue-router @4.1.5/dist/vue-router.global.js " > </ script >
</ head >
< body > Hello 👋🏻 ~ </ body >
</ html >
-
Print on the console: Howdy ~ -
Show on the page: Hello 👋🏻 ~ -
Request and execute vue.global.js -
Request and execute vue-router.global.js -
Trigger DOMContentLoaded Events
defer Property only applies to external scripts if script Script does not src , will be ignored defer Properties. defer Attribute pair module script( <script type='module'></script> )Invalid because the module script is based on defer Is loaded in the form of.
<script async>
For ordinary scripts, if the async attribute exists, the ordinary script will be requested in parallel and parsed and executed as soon as possible.
For module scripts, if the async attribute exists, the script and all its dependencies will be executed in the delay queue, so they will be requested in parallel and parsed and executed as soon as possible.
This attribute can eliminate parsing blocked Javascript.
Parsing blocked Javascript will cause the browser to load and execute the script before parsing can continue.
<! DOCTYPE html >
< html lang =" zh " >
< head >
< title > Hi </ title >
< script >
console . log ( "Howdy ~" ) ;
</ script >
< script async src =" https://google-analytics.com/analytics.js " > </ script >
< script async src =" https://ads.google.cn/ad.js " > </ script >
</ head >
< body > Hello 👋🏻 ~ </ body >
</ html >
-
Print on the console: Howdy ~ -
Parallel requests analytics.js And ad.js -
Show on the page: Hello 👋🏻 ~ -
According to the actual situation of the network, the following items will be executed out of order -
Execution analytics.js (Execute immediately after downloading) -
Execution ad.js (Execute immediately after downloading) -
Trigger DOMContentLoaded Event (may be triggered before, between and after the above two scripts)
-
async Attributes apply only to external scripts if script Script does not src , will be ignored async Properties.
summary
defer
-
Do not block the browser to parse HTML. It will not be executed until the HTML is parsed script 。 -
JavaScript resources will be downloaded in parallel. -
Scripts are executed in relative order in HTML. -
Will be triggered only after the script is downloaded and executed DOMContentLoaded Event. -
During the execution of the script, the existing elements in HTML can be obtained. defer Attribute pair Module Script Invalid. -
Applicable to: All external scripts (via src Referenced script )。
async
-
The browser is not blocked from parsing HTML, but script After the download is completed, the browser will be immediately interrupted to parse HTML and execute this script 。 -
JavaScript resources will be downloaded in parallel. -
They are independent of each other. The one who downloads first will execute first. There is no fixed sequence and they are uncontrollable. -
Since there is no specific execution time, the existing elements in HTML may not be available in the script. DOMContentLoaded Events and script The scripts are irrelevant and cannot be prioritized. -
Applicable to: independent third-party scripts.
One More Thing
-
If it exists, the browser will download this module and all its dependent modules in parallel. After all the downloads are completed, the script will be executed immediately. -
If it does not exist, the browser will download this module and all its dependent modules in parallel, and then execute this script after the browser has parsed the HTML. -
In addition, it should be noted that: set on the module script defer The property is invalid.
A picture is worth a thousand words
Thinking question 🤔
-
Why must browsers execute common script tags before parsing them? -
Ordinary script tags will block the browser from parsing HTML. What problems will this cause?
reference resources
-
https://javascript.info/script-async-defer -
https://www.growingwiththeweb.com/2014/02/async-vs-defer-attributes.html -
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script -
https://html.spec.whatwg.org/multipage/scripting.html The article was reprinted from: https://github.com/mrlmx/blogs/issues/4