How to make JavaScript execute after page load?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
In web development, particularly when working with JavaScript, there are instances where you need to execute scripts after the entire web page has loaded completely. This ensures that all elements are available for manipulation, and can help prevent unexpected errors and improve user experience. Here we will explore several techniques on how to make JavaScript execute after the page load.
Understanding the DOMContentLoaded and load Events
DOMContentLoaded Event
The DOMContentLoaded event fires when the initial HTML document has been completely loaded and parsed, without waiting for stylesheets, images, and subframes to finish loading. This is a key moment to initiate JavaScript that modifies the DOM.
Example:
load Event
The load event is used to detect a fully-loaded page, including all dependent resources such as stylesheets and images. This is typically used for tasks that require the dimensions of images or other elements that can only be calculated after these resources have fully loaded.
Example:
Executing JavaScript Using Inline Script with defer
Another method to ensure your JavaScript runs after the HTML document is loaded is by using the defer attribute in the <script> tag. Scripts with defer will execute in the order they appear in the document and only after the document has parsed.
Example:
Using the async Attribute
The async attribute can also be used but with a different use case in mind. It does not guarantee that the page has loaded. It purely allows the script to be executed asynchronously as soon as it’s available.
Example:
Comparison Table of Different Techniques
| 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 |
Practical Use Cases and Considerations
When deciding which method to use, consider what your script does and what other resources it depends on:
- Use
DOMContentLoadedif your JavaScript needs to manipulate DOM elements but does not need to wait for images or stylesheets. - Use the
loadevent for operations that require the dimensions of elements or fully loaded resources. - Use
deferfor external scripts that need to run after the HTML parse but are not dependent on CSS or images. - Use
asyncfor scripts that are independent and can run at any point during page load without affecting the rendering.
Handling JavaScript Execution in jQuery
For those using jQuery, the library offers a simple method to run code after the DOM is ready:
However, with the advent of native DOM APIs like DOMContentLoaded, the necessity to use jQuery for this purpose has diminished.
Conclusion
Choosing the right technique to control when JavaScript executes in the context of your web page's load process is crucial for creating efficient, reliable, and user-friendly web applications. Each method discussed serves different purposes and comes with its advantages. Understanding these nuances enables web developers to harness the full potential of JavaScript in web development.

