JavaScript
Web Development
Programming
Page Load
Coding Tips

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.

javascript
document.addEventListener('DOMContentLoaded', function() {
   console.log('Document is ready!');
});

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.

javascript
window.addEventListener('load', function() {
    console.log('Page fully loaded');
});

The defer attribute

Scripts with defer run after the document has been parsed, in the order they appear in the document:

html
<script src="script.js" defer></script>

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.

html
<script src="script.js" async></script>

Comparing the options

TechniqueDescriptionUse Case
DOMContentLoadedFires when initial HTML is fully parsed. Does not wait for CSS or images.Modifying or interacting with the DOM
Load EventFires when the entire page and dependencies (CSS, images) are fully loaded.Actions needing fully loaded resources
DeferExecutes script after HTML parse but before DOMContentLoaded event in order they appear.Running scripts dependent on entire DOM parse
AsyncExecutes script asynchronously as soon as it is available.Non-dependent scripts enhancing performance

Pick based on what the script depends on:

  • DOMContentLoaded when the code manipulates DOM elements but does not need images or stylesheets.
  • load when it needs element dimensions or fully loaded resources.
  • defer for external scripts that should run after the HTML is parsed but do not depend on CSS or images.
  • async for 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:

javascript
$(document).ready(function() {
    console.log('DOM is ready!');
});

With native DOMContentLoaded available, there is little need for jQuery just for this.


Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Basis Lab
No 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 course
Data 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.

Practice algorithms