Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • HTML Tutorial
  • HTML Exercises
  • HTML Tags
  • HTML Attributes
  • Global Attributes
  • Event Attributes
  • HTML Interview Questions
  • HTML DOM
  • DOM Audio/Video
  • HTML 5
  • HTML Examples
  • Color Picker
  • A to Z Guide
  • HTML Formatter
Open In App
Next Article:
HTML autocomplete Attribute
Next article icon

HTML async Attribute

Last Updated : 09 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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.'); }); 


  • The <script> tag includes the async attribute, instructing the browser to download and execute the external-script.js file asynchronously.
  • The JavaScript file contains a simple console.log statement that runs when the DOM content is fully loaded.

Note:

  • async only works with external scripts (src is required).
  • It improves page loading speed by running scripts in parallel with the rest of the page’s content.

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:

  • The dynamic-loader.js script creates a new <script> element, sets its src to dynamic-script.js, and assigns the async attribute to load it asynchronously.
  • The dynamic-script.js file contains a console.log statement indicating it has been loaded.

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:

  • Three external scripts are included in the HTML, each with the async attribute.
  • Each script contains a console.log statement indicating its loading.
  • The scripts will load and execute asynchronously, and their execution order is not guaranteed.

Best Practices for HTML async Attribute

  • Use async for independent scripts that don’t rely on other scripts or the DOM, such as analytics or advertising scripts.
  • Avoid using async for scripts that depend on the DOM or other scripts to ensure proper execution order.


Next Article
HTML autocomplete Attribute

S

shubham_singh
Improve
Article Tags :
  • HTML
  • Web Technologies
  • HTML-Attributes

Similar Reads

  • HTML accept Attribute
    HTML accept Attribute specifies the type of file that the server accepts. This attribute can be used with <input> element only. This attribute is not used for validation tools because file uploads should be validated on the Server. Syntax: <input accept = "file_extension"> Note: This att
    3 min read
  • HTML accept-charset Attribute
    The accept-charset attribute is used to define the character encoding and is used for form submission. The default value of the accept-charset attribute is "UNKNOWN" string which indicates the encoding equals to the encoding of the document containing the <form> element. Syntax: <form accep
    2 min read
  • HTML accesskey Attribute
    The HTML accesskey attribute defines a keyboard shortcut to activate or focus an element. It assigns a keyboard key combination to trigger an element (like a link or button).Browser support and specific key combinations vary (e.g., Alt + key on Windows, Ctrl + key on macOS).Use it sparingly and prov
    3 min read
  • HTML action Attribute
    The HTML action attribute is used to specify where the form data should be sent on submission. It allows the browser to send the data to the specified location, enabling server-side scripts to process the data and generate a response. Note: It can be used in the <form> element. Syntax:<form
    3 min read
  • HTML align Attribute
    In HTML, the align attribute is used to control the alignment of elements on a webpage. Whether it's for text, images, or tables, the align attribute helps to position content in relation to its surrounding elements. Syntax <element_name align="left | right | center | justify">Attribute Values
    4 min read
  • HTML alt attribute
    The alt attribute in HTML provides alternative text for images, aiding accessibility and providing context for screen readers. Syntax: <img src=" " alt=" " >[GFGTABS] HTML <html> <body> <h1>GeeksforGeeks Logo</h1> <img src="https://media.geeksforgeeks.org/wp-con
    3 min read
  • HTML async Attribute
    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></sc
    3 min read
  • HTML autocomplete Attribute
    The HTML autocomplete Attribute is used to specify whether the input field autocompleted would be on or off. When the autocomplete attribute is set to on the browser will automatically complete the values based on what the user entered before. It works with input fields such as text, search, URL, em
    3 min read
  • HTML autoplay Attribute
    The HTML autoplay Attribute, a boolean attribute, enables audio or video elements to start playing automatically when the page loads, providing seamless playback without interruption. Syntax: <element autoplay> Supported ElementsIt can be used with <audio> and <video> elements.  El
    2 min read
  • HTML autofocus Attribute
    The HTML autofocus attribute is a powerful tool that enhances user experience by automatically setting the focus to a specific element when a webpage loads. Syntax<input type="text" autofocus> Note: This attribute is boolean, meaning it can either be present or absent. Supported TagsElementPur
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences