Table of Contents
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.
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Load Events</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>
<h4>DOMContentLoaded</h4>
<p id="demo"></p>
<script>
document.addEventListener("DOMContentLoaded", function() {
document.getElementById("demo").innerHTML = "DOM fully loaded and parsed!";
});
</script>
</body>
</html>

Result View Example
The window.onload event fires when the entire page loads including all content (images, CSS, scripts, etc.).
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Load Events</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>
<h4>Window Load</h4>
<p id="demo"></p>
<script>
window.onload = function() {
document.getElementById("demo").innerHTML = "Window fully loaded (including images)!";
};
</script>
</body>
</html>

Result View Example
Here are other load-related events:
| Event | Description |
|---|---|
DOMContentLoaded |
Fires when the HTML document is loaded and parsed |
load |
Fires when the entire page (including resources) has loaded |
beforeunload |
Fires when the document is about to be unloaded |
unload |
Fires once a page has unloaded (or the browser window has been closed) |
error |
Fires when an error occurs while loading an external resource |
The load event can be used on images to know when an image has finished loading:
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Load Events</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>
<h4>Image Load</h4>
<img id="myImg" src="https://www.w3schools.com/js/pic_bulboff.gif" width="100" height="180">
<p id="demo"></p>
<script>
document.getElementById("myImg").onload = function() {
document.getElementById("demo").innerHTML = "Image loaded successfully!";
};
</script>
</body>
</html>

Result View Example
Document in project
You can Download PDF file.