乐闻世界logo
搜索文章和话题

What is the purpose of the 'async' and 'defer' attributes in the script tag?

2024年7月18日 00:34
  • async attribute: When the async attribute is used, the script file is loaded asynchronously. This means the script file is downloaded concurrently with HTML parsing, but the execution order of scripts is not guaranteed. Once the script is downloaded, it is executed immediately, and HTML parsing may be temporarily halted until the script execution is complete.
  • defer attribute: When the defer attribute is used, the script file is also loaded asynchronously, but the execution of the script is postponed until the entire HTML document has been parsed. This ensures that the execution order of scripts follows the order in which they appear in the HTML document.

Overall, both attributes aim to improve page load time and user experience. However, async is suitable for scenarios where scripts are independent of each other and the execution order is not critical, while defer is used when the execution order needs to be guaranteed.

标签:PWA