html tutorial - defer Attribute in HTML - html5 - html code - html form



 defer attribute in html

Learn html - html tutorial - defer attribute in html- html examples - html programs

  • The defer attribute is a Boolean attribute.
  • The defer attribute specifies that the script is executed after the page has finished parsing.

Syntax for defer attribute in HTML:

<script defer>

Applies To:

Elements Attribute
<script> defer

Sample Coding for defer Attribute in HTML:

Tryit<!DOCTYPE html>
<html>
    <head>
        <title>Wikitechy defer attribute</title>
    </head>
    <body>
         <script src="script.js" defer>
         </script>  
         <p>The script is executed before the paragraph exists.</p>
         <p id="p1">Wikitechy</p>
         <p> This way the script can request information from the paragraph.</p>
         </body>
</html>

Sample Coding for defer attribute script.js:

alert(document.getElementById(“p1”).firstChild.nodeValue);

Code Explanation for defer Attribute in HTML:

 defer Attribute Code Explanation

  1. defer attribute is used to specify the script file must be load after parsing the page content
 defer1 Attribute Code Explanation

  1. The alert box will be display the value of the element which has id as “p1”.

Output for defer Attribute in HTML:

 defer Attribute Tag Output

  1. The content is written inside the <p> tag as it is shown in the output.
  2. alert dialog box shows “Wikitechy”. If we did not declare the defer attribute the value cannot be displayed in the alert box, because the script file declared before the content.

Browser Support for defer Attribute in HTML:

yes 10.0 3.6 Yes 15.0

Tips and Notes

  • The defer attribute is only used in external <script> element.

Related Searches to defer Attribute in html