A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/html/html-async-attribute/ below:

HTML async Attribute - GeeksforGeeks

HTML async Attribute

Last Updated : 09 Jan, 2025

The HTML <script> async attribute is used to load and execute external scripts without blocking the rest of the page from loading. This speeds up page load times. It only works with external scripts (when the src attribute is present).

Syntax:
<script src="path-to-script.js" async></script>
html
<!DOCTYPE html>
<html >
  <body>
    <h1>Welcome to My Website</h1>
    <p>This page demonstrates loading an external script asynchronously.</p>
    <script src="external-script.js" async></script>
  </body>
</html>

JavaScript (external-script.js):

JavaScript
document.addEventListener('DOMContentLoaded', function() {
  console.log('External script loaded asynchronously.');
});

Note:

More Example of HTML async Attrribute

Dynamically Adding an Asynchronous Script with JavaScript HTML
<html lang="en">
  <body>
    <p>This page demonstrates adding a script asynchronously using JavaScript.</p>
    <script src="dynamic-loader.js"></script>
  </body>
</html>

JavaScript (dynamic-loader.js):

JavaScript
let script = document.createElement('script');
script.src = 'dynamic-script.js';
script.async = true;
document.head.appendChild(script);

JavaScript (dynamic-script.js):

console.log('Dynamic script loaded asynchronously.');

In this example:

Loading Multiple Scripts with async Attribute HTML
<html>
<body>
	<p>This page demonstrates loading multiple external scripts asynchronously.</p>
	<script src="script1.js" async></script>
	<script src="script2.js" async></script>
	<script src="script3.js" async></script>
</body>
</html>

JavaScript (script1.js):

console.log('Script 1 loaded.');

JavaScript (script2.js):

console.log('Script 2 loaded.');

JavaScript (script3.js):

console.log('Script 3 loaded.');

In this example:

Best Practices for HTML async Attribute

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4