HTML defer Attribute

HTML defer Attribute: This attribute specifies the Boolean attribute. If the defer attribute is present it specifies that the script is executed when the page had finished the parsing.

HTML defer Attribute

This attribute can be applied to the <script> element. And this attribute is only for external scripts. (i.e if the src attribute is present)

NOTE:-



  • If async is present: The script will be executed asynchronously with the rest of the page.
  • If defer is present and async is not present: Script will be executed when the page has finished analysis.
  • Neither sync nor defer:  Script is fetched and executed immediately before the browser continues the page.

Browser Support

This attribute is supported by the following browsers:

  • Chrome
  • Safari
  • Firefox – 3.6
  • Opera – 15.0
  • Internet Explorer – 10.0

Example: <script> element 

<!DOCTYPE html>
<html>
<body>

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

<p>The script above requests information from the paragraph below. Normally, this is not possible, because the script is executed before the paragraph exists.</p>

<p id="p1">Hello Freshersnow!</p>

<p>However, the defer attribute specifies that the script should be executed later. This way the script can request information from the paragraph.</p>

</body>
</html>

Output:

HTML defer attribute