HTML - attributes - async

revision:


Content

"async" attribute is a boolean attribute. syntax some examples


"async" attribute is a boolean attribute.

top

If it is set, the script is downloaded in parallel to parsing the page and executed as soon as it is available.
The parsing of the page is interrupted once the script is downloaded completely, and then the script is executed, before the parsing of the rest of the page continues.
The "async" attribute is only for external scripts (and should only be used if the "src" attribute is present).

Note: There are several ways an external script can be executed:

If "async" is present : the script is downloaded in parallel to parsing the page, and executed as soon as it is available (before parsing completes);
If "defer" is present (and not "async") : the script is downloaded in parallel to parsing the page, and executed after the page has finished parsing;
If neither "async" or "defer" is present : the script is downloaded and executed immediately, blocking parsing until the script is completed.

Use "defer" for most scripts, especially those that need the full HTML/DOM to be ready and/or depend on other scripts.
Use "async" only for independent scripts, like analytics (Google Analytics, etc.), ads, chat widgets


syntax

top

<script async>


some examples

top

Hello World!

codes:
                    <p id="p1">Hello World!</p>
                    <script src="demo_async.js" async></script>
                

How do you do!

codes:

                    <p id="p1">How do you do! </p>
                    <script async src="script.js"></script>