JavaScript async Attribute

HTML5 bring the async attribute for <script> elements. The async attribute is alike to defer (which you have read earlier) in that it transforms the way a script is processed. Further similar to defer attribute, async applies only to external scripts and signals which the browser begin for downloading the file immediately. Similar to that of defer, scripts marked as async does not guarantee that execution will be in the order in which they are specified.

Let us take a code snippet:

Example:
<!DOCTYPE html>
<html>
<head>
     <title>Example HTML Page</title>
     <script type="text/javascript" async src="script.js"></script>
     <script type="text/javascript" async src="script1.js"></script> 
</head>
<body>
     <!— body content here -->
</body>
</html>

In the above code, the second script file might start execution before the first, and so it is vital that there are no dependencies among the two. The point of specifying an async script is to designate that the page need not have to wait for the script to which has to be downloaded and then executed before continuing to load it also need not wait for another script to load and execute before it can do the same.


Scroll Back to Top