HTML async Attribute

The async attribute is a boolean attribute and specifies that the script will be executed asynchronously once it is available. It only works for external scripts and must be used only when the src attribute is present.

You can use the async attribute on the <script> element.

An external script can be executed in the following ways:

  • When async is present, the script will be executed asynchronously while the page continues the parsing.
  • When async is not present but defer is present, the script will be executed when the page finishes the parsing.
  • When neither async or defer is present, the script will be executed immediately before the browser continues the parsing.

Syntax

<script src="example.js" async></script>

Example of the HTML async attribute:

<!DOCTYPE html>
<html>
  <head>
    <title>Title of the documnet</title>
    <script src="example.js" async></script>
    <noscript>Sorry, your browser does not support JavaScript!</noscript>
  </head>
  <body>
    <h1>Example</h1>
    <p>
      A browser that doesn’t support JavaScript will display the content inside the noscript element.
    </p>
    <script>
      document.write("My first JavaScript example!")
    </script>
  </body>
</html>

Practice Your Knowledge

What is the function of the 'async' attribute in HTML?

Quiz Time: Test Your Skills!

Ready to challenge what you've learned? Dive into our interactive quizzes for a deeper understanding and a fun way to reinforce your knowledge.

Do you find this helpful?