Differences between async and defer attributes in script tags of interview questions
First of all, we should write the script tag in the body, so that the browser will not parse JS when loading HTML documents, which will prevent HTML rendering.
How to optimize the loading of third party scripts?
One way is to add
<script async> <script defer>
The async attribute is executed after the script is downloaded and before the load event of window. If the documents are not parsed at this time, it means that they can prevent DOM construction.
The defer attribute is executed after the document is fully parsed, before the domcontentloaded event in the window. The defer property ensures that they are executed in the order in which they appear in HTML, and does not prevent the main thread from rendering.
- This script can be used if it needs to be executed early in the loading process
- If this script is not that important, it can be used