HTML defer Attribute

The HTML defer attribute specifies that a script is executed when a page has finished the parsing. It is a boolean attribute. This attribute works for external scripts and must be used only when the src attribute is present.

There isn’t any difference between HTML 4.01 and HTML5. In XHTML, the defer attribute must be specified as <script defer=”defer”> as attribute minimization is forbidden.

You can use this attribute on the <script> element.

An external script can be executed in the following ways:

  • When async is present, the script will be executed asynchronously while the page continues the parsing.
  • When async is not present but defer is present, the script will be executed when the page finishes the parsing.
  • When neither async or defer is present, the script will be executed immediately before the browser continues the parsing.

Syntax

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

Example of the HTML defer attribute:

<!DOCTYPE html>
<html>
  <head>
    <title>Title of the documnet</title>
    <script src="example.js" defer></script>
    <noscript>Sorry, your browser doesn't support JavaScript!</noscript>
  </head>
  <body>
    <h1>Example</h1>
    <p>The "defer" attribute  specifies that a script is executed when a page has finished the parsing. It is a boolean attribute.</p>
  </body>
</html>

Practice Your Knowledge

What is the correct use of the 'defer' attribute in HTML?

Quiz Time: Test Your Skills!

Ready to challenge what you've learned? Dive into our interactive quizzes for a deeper understanding and a fun way to reinforce your knowledge.

Do you find this helpful?