Sometimes, we want to listen for DOM ready event with JavaScript.
In this article, we’ll look at how to listen for DOM ready event with JavaScript.
How to listen for DOM ready event with JavaScript?
To listen for DOM ready event with JavaScript, we listen to the DOMContentLoaded
event.
For instance, we write
const onReady = () => {
/* ... */
};
document.addEventListener("DOMContentLoaded", onReady);
to call addEventListener
to listen for the DOMContentLoaded
event.
We set onReady
as its event listener.
It’s called when the DOM is done loading.
Conclusion
To listen for DOM ready event with JavaScript, we listen to the DOMContentLoaded
event.