1

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
  • 75
  • 6

1 Answers1

1
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
  • 17,318
  • 4
  • 30
  • 54