HTML defer Attribute Last Updated : 18 Dec, 2024 Comments Improve Suggest changes Like Article Like Report 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 WorksWhen 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 AttributeScripts 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. Comment More infoAdvertise with us V Vijay Sirra Follow Improve Article Tags : Web Technologies HTML HTML-Attributes Explore HTML Tutorial 11 min read BasicsHTML Introduction 5 min read HTML Editors 5 min read HTML Basics 7 min read HTML Structure & ElementsHTML Elements 5 min read HTML Attributes 8 min read HTML Headings 4 min read HTML Paragraphs 5 min read HTML Text Formatting 4 min read HTML Block and Inline Elements 3 min read HTML Charsets 4 min read HTML ListHTML Lists 5 min read HTML Ordered Lists 5 min read HTML Unordered Lists 4 min read HTML Description Lists 3 min read HTML Visuals & MediaHTML Colors 11 min read HTML Links Hyperlinks 3 min read HTML Images 7 min read HTML Favicon 4 min read HTML Video 4 min read HTML Layout & DesignHTML Tables 10 min read HTML Iframes 4 min read HTML Layout 4 min read HTML File Paths 3 min read HTML Projects& Advanced TopicsHTML Forms 5 min read HTML5 Semantics 6 min read HTML URL Encoding 4 min read HTML Responsive Web Design 11 min read Top 10 Projects For Beginners To Practice HTML and CSS Skills 8 min read HTML Tutorial ReferencesHTML Tags - A to Z List 15+ min read HTML Attributes Complete Reference 8 min read HTML Global Attributes 5 min read HTML5 Complete Reference 8 min read HTML5 MathML Complete Reference 3 min read HTML DOM Complete Reference 15+ min read HTML DOM Audio/Video Complete Reference 2 min read SVG Element Complete Reference 5 min read SVG Attribute Complete Reference 8 min read SVG Property Complete Reference 7 min read HTML Canvas Complete Reference 4 min read Like