HTML defer Attribute
The HTML defer attribute specifies that a script is executed when a page has finished the parsing. Read and find out how to use it on the <script> element.
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.
In HTML 4.01, the behavior of the defer attribute was implementation-dependent, whereas HTML5 standardized it. 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, in the order they appear in the document.
- When neither async nor defer is present, the script will be executed immediately before the browser continues the parsing.
Syntax
Syntax of HTML defer Attribute
<script src="example.js" defer></script>Example of the HTML defer attribute:
<!DOCTYPE html>
<html>
<head>
<title>Title of the document</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
What is the correct use of the 'defer' attribute in HTML?