HTML <script> asyncAttributes

HTML script 标签参考手册HTML <script> Tag

Examples

The script is executed immediately once it is loaded:

<script src="demo_async.js.html" async></script>

Try it Yourself »

Browser Support

Internet ExplorerFirefoxOperaGoogle ChromeSafari

Internet Explorer 10, Firefox, Opera, Chrome and Safari support the async attribute.

Note:Internet Explorer 9 and earlier versions do not support the async attribute of the <script> tag.


Definition and Usage

The async attribute is a boolean attribute.

The async attribute can be used to control the asynchronous loading of scripts and execute them immediately after loading is complete (asynchronous loading, synchronous execution).

When the async attribute is set on the <script> tag, the browser loads the script file asynchronously and executes the script immediately after the download is complete, without waiting for the rendering of the page. This prevents the download and execution of scripts from blocking page rendering, thereby improving page load speed.

Note:Because setting the async attribute no longer guarantees the execution order of scripts, you need to pay attention to dependencies between scripts when writing code. If there are dependencies between multiple scripts, you need to use the defer attribute or manually manage the loading order to ensure the correct execution order of scripts.

Note:The async attribute only applies to external scripts (only when the src attribute is used).

Note:There are several ways to execute external scripts:

  • If async="async": The script is executed asynchronously relative to the rest of the page (the script is executed while the page continues parsing)
  • If async is not used and defer="defer": The script is executed when the page has finished parsing
  • If neither async nor defer is used: The script is fetched and executed immediately before the browser continues parsing the page

Differences between HTML 4.01 and HTML5

The async attribute is new in HTML5.


Differences between HTML and XHTML

In XHTML, attribute minimization is forbidden, and the async attribute must be defined as <script async="async">.


Syntax

<script async>


HTML script 标签参考手册HTML <script> Tag Other Extensions