Anjan Dutta

How to check if DOM content is loaded in Javascript

How to check if DOM content is loaded in Javascript

Created On: 02/10/2021

To check if DOM content is loaded in javascript, we have to create an event listener and bind that listener to the DOMContentLoaded event.

Once that event is created, the listener will be triggered and an associated function will be called.

See the code below:

document.addEventListener("DOMContentLoaded", function testDom() {
console.log("DOM content loaded");
});

And below is the working code.

<html>
<head>
<script>
document.addEventListener("DOMContentLoaded", function testDom() {
console.log("DOM content loaded");
});
</script>
</head>
<body>
<span>
Testing DOM ready state!
</span>
</body>
</html>