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?
Preface Let me share with you how to make a searc...
This article shares the specific code for JavaScr...
What is the role of http in node The responsibili...
The Docker images we usually build are usually la...
Before reading this article, I hope you have a ba...
What is Load Balancing Load balancing is mainly a...
Preface Linux groups are organizational units use...
Table of contents Install jupyter Docker port map...
Using Navicat directly to connect via IP will rep...
The Golden Rule No matter how many people are wor...
0. Introduction What is the ibdata1 file? ibdata1...
For Linux system administrators, it is crucial to...
This article uses examples to describe the three ...
1. Technical points involved vite version vue3 ts...
Description: Limit the number of lines of text di...