Run JavaScript after page load: DOMContentLoaded, load and defer
Codemia Knowledge Hub ·
Listen for DOMContentLoaded to run code as soon as the HTML is parsed, or for the window load event if you also need images and stylesheets to finish loading. For an external script, adding defer to the <script> tag runs it after the document is parsed.
DOMContentLoaded: after the HTML is parsed
DOMContentLoaded fires when the initial HTML document has been completely loaded and parsed, without waiting for stylesheets, images and subframes. It is the right moment for code that modifies the DOM.
load: after every resource has loaded
The load event fires when the whole page has loaded, including stylesheets and images. Use it for work that needs image dimensions or other values that are only known once those resources have loaded.
The defer attribute
Scripts with defer run after the document has been parsed, in the order they appear in the document:
The async attribute
async serves a different purpose. It does not wait for the page to load; the script runs asynchronously as soon as it is available.
Comparing the options
| Technique | Description | Use Case |
| DOMContentLoaded | Fires when initial HTML is fully parsed. Does not wait for CSS or images. | Modifying or interacting with the DOM |
| Load Event | Fires when the entire page and dependencies (CSS, images) are fully loaded. | Actions needing fully loaded resources |
| Defer | Executes script after HTML parse but before DOMContentLoaded event in order they appear. | Running scripts dependent on entire DOM parse |
| Async | Executes script asynchronously as soon as it is available. | Non-dependent scripts enhancing performance |
Pick based on what the script depends on:
DOMContentLoadedwhen the code manipulates DOM elements but does not need images or stylesheets.loadwhen it needs element dimensions or fully loaded resources.deferfor external scripts that should run after the HTML is parsed but do not depend on CSS or images.asyncfor independent scripts that can run at any point during page load without affecting rendering.
jQuery's $(document).ready()
jQuery has its own way to run code once the DOM is ready:
With native DOMContentLoaded available, there is little need for jQuery just for this.
Related reading
- How to make join queries using Sequelize on Node.js
- How to make layout with rounded corners..?
- how to make synchronous call to indexeddb method from javascript
- how to make synchronous http calls within async.each in nodejs
- How to make the corners of a button round?
- How to make Twitter Bootstrap menu dropdown on hover rather than click
- How to manage a redirect request after a jQuery Ajax call
- How to open a Bootstrap modal window using jQuery?
System design interview problems on this topic
- Design Google Doc: solution and walkthrough
- Design Facebook Messenger: solution and walkthrough
- Design Typeahead Suggestion: solution and walkthrough
- Design an Online Presence Indicator Service: solution and walkthrough
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo course covers this one yet
Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.
Build my courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.