What is Async and Defer?
Async and Defer are two attributes that can be used in the
<script>
tag to optimize the loading of JavaScript files. Async and Defer are both used to tell the browser to download the JavaScript file while the HTML document is still being parsed. The difference between the two is that Async will execute the JavaScript file as soon as it is downloaded, while Defer will wait until the HTML document has been completely parsed before executing the JavaScript file.
How to Use Async and Defer
Using Async and Defer is simple. All you need to do is add the attribute to the
<script>
tag. For example, if you wanted to use Async, you would add the attribute
async
to the
<script>
tag like this:
<script src="example.js" async></script>
If you wanted to use Defer, you would add the attribute
defer
to the
<script>
tag like this:
<script src="example.js" defer></script>
Benefits of Using Async and Defer
Using Async and Defer can help improve the performance of your website by allowing the browser to download and execute JavaScript files while the HTML document is still being parsed. This can help reduce the amount of time it takes for the page to load, as the browser does not have to wait for the JavaScript files to be downloaded and executed before it can continue parsing the HTML document.
Summary
Using Async and Defer can help improve the performance of your website by allowing the browser to download and execute JavaScript files while the HTML document is still being parsed. This can help reduce the amount of time it takes for the page to load, as the browser does not have to wait for the JavaScript files to be downloaded and executed before it can continue parsing the HTML document.
Leave a Reply