2

I would like to execute some javascript code on a web-page after it has loaded, but only the DOM.

I have seen that in the past you could use the document.onload event but that it is no longer supported by all the browsers.

I have stumbled over this piece of code :

window.onload = function() {
 //code to be executed after the web-page is done loading...
}

The issue is that this waits for the full web-page to load i.e. images and other stuff included.

Is there any other way to do this ?

abiday
  • 85
  • 6

1 Answers1

2
window.addEventListener('DOMContentLoaded', (event) => {
    console.log('DOM fully loaded and parsed');
});

https://developer.mozilla.org/en-US/docs/Web/API/Window/DOMContentLoaded_event

Adrian Brand
  • 20,384
  • 4
  • 39
  • 60