Fill in the blank: Add the ________ attribute to your script to tell the browser to execute it after construction of the DOM.
- defer
- preload
- async
- integrity
Explanation: The correct answer is defer. When you include the defer
attribute in your script tag, it instructs the browser to download the script while parsing the HTML document but to execute it only after the HTML document has been fully parsed and the DOM (Document Object Model) has been constructed. This means that the script will be executed in the order it appears in the document, after all the elements in the HTML have been loaded and before the DOMContentLoaded
event is fired. This attribute is particularly useful when you have scripts that rely on the DOM being fully loaded before they execute, ensuring that they don’t interfere with the rendering of the page or cause errors by trying to access elements that haven’t been created yet. It helps improve page loading performance by allowing the browser to download scripts asynchronously while still preserving their order of execution, which can lead to a better user experience.