The Wayback Machine - https://web.archive.org/web/20250323001548/https://www.geeksforgeeks.org/html-defer-attribute/
Open In App

HTML defer Attribute

Last Updated : 18 Dec, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

The defer attribute can be added to <script> elements that point to an external JavaScript file. It ensures that the script is executed only after the HTML document has been completely loaded and parsed, but before the DOMContentLoaded event is fired.

  • It ensures that scripts do not block HTML parsing.
  • By deferring the script, the browser can render content faster.
  • The defer attribute only works with external scripts, not inline ones.
html
<html>
<body>
    <script id="myGeeks"
            type="text/javascript"
            src="my_script.js"
            defer>
    </script>
    <br>
    <button type="button" onclick="myFunction()">Submit</button>
</body>
</html>
JavaScript
function myFunction() {
    alert("Script works");
}

In this example:

  • The script my_script.js is linked in the <body>, but its execution is deferred until after the HTML document is parsed.
  • The page content will be rendered first, and once the HTML is fully loaded, the script will execute.

Syntax

<script src="script.js" defer></script>

How It Works

When the browser encounters a <script> tag with the defer attribute:

  • The script is downloaded in parallel to the HTML parsing.
  • The script’s execution is deferred until the HTML parsing is complete.
  • The script executes in the same order as it appears in the document.

Example Without defer (Blocking Script):

HTML
<html>
<head>
    <script src="script.js"></script>
</head>
<body>
    <h1>This text is delayed!</h1>
         <button type="button" onclick="myFunction()"> Submit </button>
</body>
</html>
JavaScript
function myFunction() {
    alert("Script works");
}
  • The browser halts HTML parsing to download and execute script.js.
  • The <h1> element rendering is delayed until the script executes.

Example With defer:

HTML
<html>
<head>
  <script src="script.js" defer></script>
</head>
<body>
    <h1>This text loads without delay!</h1>
         <button type="button" onclick="myFunction()">Submit</button>
</body>
</html>
JavaScript
function myFunction() {
    alert("Script works");
}
  • The browser parses and renders the HTML first.
  • The script executes only after the entire document is parsed.

Why Use the defer Attribute?

  • Improved Performance: By deferring the script execution, the browser can parse and render the HTML without waiting for the script to load and execute.
  • Better User Experience: Deferring JavaScript allows the page content to load and become interactive sooner, reducing perceived load times.
  • Avoids Render-Blocking: Without defer, scripts placed in the <head> can block the browser from rendering the page until the script is fully loaded and executed.

Key Features of the defer Attribute

  • Scripts with defer are downloaded during the parsing of the HTML but are executed only after the entire document has been parsed.
  • Deferred scripts are executed in the order they appear in the document, ensuring predictable behavior.
  • The defer attribute works only with external JavaScript files (specified using the src attribute). Inline scripts will ignore defer.
  • A script cannot have both async and defer attributes. If both are present, async takes precedence, and defer is ignored.

HTML defer Attribute -FAQs

How does the defer attribute affect script loading?

The defer attribute ensures that the script is downloaded asynchronously in the background while the HTML is being parsed, but execution of the script is delayed until after the HTML document is fully parsed.

What is the difference between defer and async attributes in HTML?

defer ensures that the script is executed in order after the HTML is fully parsed, whereas async allows the script to be executed as soon as it is downloaded, which may happen before or after the document is parsed.

When should you use the defer attribute?

Use defer when you want to load JavaScript files without blocking the HTML parsing, and when the script depends on the entire HTML document being parsed first (e.g., manipulating the DOM).

Can the defer attribute be used with inline scripts?

No, the defer attribute is only applicable to external scripts loaded using the src attribute. It does not work with inline scripts directly written between <script> tags.

Can the defer attribute be used with module type scripts?

Yes, the defer attribute is automatically applied to scripts with type=”module”, meaning they are executed in the same way as scripts with defer, after the HTML parsing is complete.



Next Article

Similar Reads

three90RightbarBannerImg