HTML async Attribute
Description
The async
attribute is used in HTML to indicate that a script should be executed asynchronously as soon as it is available. This attribute can be added to the <script>
tag to control the loading and execution of JavaScript files. Unlike the default behavior, where scripts are loaded and executed in the order they appear in the document, causing the parsing of the document to be blocked until the script is downloaded and executed, scripts with the async
attribute are executed independently of the page's load process. This means that the page does not wait for the asynchronous script to load and execute, allowing the browser to continue parsing and rendering the rest of the document without interruption.
The async
attribute is particularly useful for scripts that do not depend on other scripts and do not modify the DOM (Document Object Model) immediately upon loading. Including the async
attribute in a script tag looks like this: <script async src="path/to/script.js"></script>
. When a script is loaded with async
, it's executed as soon as it is downloaded, which could be before or after the HTML document is fully parsed. This improves the loading performance of web pages but requires developers to be mindful of any dependencies between scripts or on the readiness of the DOM.
Syntax
<script src="URL" async>
Values
- URLURL or path of the source file.
Applies To
The async
attribute can be used on the following html elements.
Example
<script async src="myscript.js"></script>
Browser Support
The following table will show you the current browser support for the HTML async
Attribute.
Desktop | |||||
Yes | Yes | Yes | Yes | Yes |
Tablets / Mobile | |||||
Yes | Yes | Yes | Yes | Yes | Yes |
Last updated by CSSPortal on: 28th March 2024