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 Next Article HTML defer Attribute V Vijay Sirra Follow Improve Article Tags : Web Technologies HTML HTML-Attributes Similar Reads HTML accept Attribute HTML accept Attribute specifies the type of file that the server accepts. This attribute can be used with <input> element only. This attribute is not used for validation tools because file uploads should be validated on the Server.Syntax:<input accept = "file_extension"> Note: This attri 3 min read HTML accept-charset Attribute The accept-charset attribute is used to define the character encoding and is used for form submission. The default value of the accept-charset attribute is "UNKNOWN" string which indicates the encoding equals to the encoding of the document containing the <form> element. Syntax:<form accept 2 min read HTML accesskey Attribute The HTML accesskey attribute defines a keyboard shortcut to activate or focus an element.It assigns a keyboard key combination to trigger an element (like a link or button).Browser support and specific key combinations vary (e.g., Alt + key on Windows, Ctrl + key on macOS).Use it sparingly and provi 3 min read HTML action Attribute The HTML action attribute is used to specify where the form data should be sent on submission. It allows the browser to send the data to the specified location, enabling server-side scripts to process the data and generate a response.Note: It can be used in the <form> element. Syntax:<form 3 min read HTML align Attribute In HTML, the align attribute is used to control the alignment of elements on a webpage. Whether it's for text, images, or tables, the align attribute helps to position content in relation to its surrounding elements.Syntax<element_name align="left | right | center | justify">Attribute ValuesAt 4 min read HTML alt attribute The alt attribute in HTML provides alternative text for images, aiding accessibility and providing context for screen readers.Syntax:<img src=" " alt=" " >HTML<html> <body> <h1>GeeksforGeeks Logo</h1> <img src="https://media.geeksforgeeks.org/wp-content/uploads/20190 3 min read HTML async Attribute The HTML <script> async attribute is used to load and execute external scripts without blocking the rest of the page from loading. This speeds up page load times. It only works with external scripts (when the src attribute is present).Syntax:<script src="path-to-script.js" async></scr 3 min read HTML autocomplete Attribute The HTML autocomplete Attribute is used to specify whether the input field autocompleted would be on or off. When the autocomplete attribute is set to on the browser will automatically complete the values based on what the user entered before. It works with input fields such as text, search, URL, em 3 min read HTML autoplay Attribute The HTML autoplay Attribute, a boolean attribute, enables audio or video elements to start playing automatically when the page loads, providing seamless playback without interruption.Syntax: <element autoplay> Supported ElementsIt can be used with <audio> and <video> elements. Elem 2 min read HTML autofocus Attribute The HTML autofocus attribute is a powerful tool that enhances user experience by automatically setting the focus to a specific element when a webpage loads. Syntax<input type="text" autofocus> Note: This attribute is boolean, meaning it can either be present or absent.Supported TagsElementPurp 2 min read Like