Let me start with a question: When writing an HTML page, when you want to import a JS file from the outside, where will the script tag be placed? Does the different placement have an impact on page loading?
<script src="./1.js" async></script> <script src="./1.js" defer></script> async and deferAsynchronous means that if the defer or async attribute is turned on in the script tag, the script will be loaded asynchronously. When the browser rendering engine encounters this line of command, it will start downloading the external script . While downloading, the rendering engine will directly execute the subsequent commands.
The difference between the async attribute and defer is: The blue line represents network reads (script downloads), the red line represents execution, both of which are for scripts; the green line represents HTML parsing. defer attribute, the browser will download the corresponding script immediately. The page processing will not stop during the download process. The script will not be executed until the document parsing is completed . async attribute, the browser will download the corresponding script immediately. The page processing will not stop during the download process. It will be executed immediately after the download is completed . The page processing will stop during the execution process. If no attributes are set, then when a script is encountered, the page processing will continue after the script is downloaded and executed. [ Note ] async is more powerful than defer. When the same tag uses both attributes at the same time, follow async! ! ! Multiple scripts The difference between async and defer is not only reflected in the download and execution of external script files, but also in the difference when multiple scripts exist: External script files // ... a lot of js code console.log('1'); 2.js file: console.log('2'); Main html file Use defer : <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>js blocking</title> <!-- defer will let dom execute first --> <script src="./1.js" defer></script> <script src="./2.js" defer></script> </head> <body> How does <h1>js blocking work? </h1> <script> document.addEventListener('DOMContentLoaded', function() { console.log('DOMContentLoaded'); }) </script> </body> </html> Console execution results: Using async : <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>js blocking</title> <!-- defer will let dom execute first --> <script src="./1.js" async></script> <script src="./2.js" async></script> </head> <body> How does <h1>js blocking work? </h1> <script> document.addEventListener('DOMContentLoaded', function() { console.log('DOMContentLoaded'); }) </script> </body> </html> Console execution results: From the running results of the console, we can see: async : Whichever download is completed first will be executed immediately ! ! ! These two scripts may not be executed before the DOMContentLoaded event is triggered, but they will definitely be executed before the window.onload event. In addition, it is worth noting that during the execution of the script that has been downloaded first, other scripts will not stop downloading but will continue downloading. [ Note ] DOMContentLoaded will be triggered after DOM loading is completed, that is, after the document is fully loaded and parsed. summary
refer to [1] https://blog.csdn.net/mx18519142864/article/details/82021754 This is the end of this article about the performance optimization case of JavaScript file loading and blocking issues. For more relevant content about JavaScript file loading and blocking issues, please search for previous articles on 123WORDPRESS.COM or continue to browse the related articles below. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Detailed explanation of how to use Docker to deploy Django+MySQL8 development environment
>>: Where is mysql data stored?
console.log( [] == ![] ) // true console.log( {} ...
Link: https://qydev.weixin.qq.com/wiki/index.php?...
Docker view process, memory, cup consumption Star...
A WeakMap object is a collection of key/value pai...
This blog post is about a difficulty encountered ...
uninstall First, confirm whether it has been inst...
Install PostgreSQL 11 on CentOS 7 PostgreSQL: The...
1. Analytical thinking 1. Eliminate the machine...
IDEA is the most commonly used development tool f...
1. Prepare the Java environment, jdk1.8 Check whe...
MySQL password modification example detailed expl...
This article example shares the specific code of ...
Table of contents 1. Introduction to import_table...
Preface Sometimes I feel that the native UI of We...
1. Tcl script file circle.tcl code comments #Set ...