How to wait for DOM ready event in pure JavaScript

How to run JavaScript as soon as possible but not early

You can add event listeners todocumentObjectDOMContentLoadedevent:

document.addEventListener('DOMContentLoaded', (event) => {
  //the event occurred

I usually don’t use arrow functions for event callbacks because we can’t accessthis.

In this case, we don’t need to do this becausethisalwaysdocument. In any other event listener, I will use regular functions:

document.addEventListener('DOMContentLoaded', function(event) {
  //the event occurred

For example, if I add an event listener to the loop, but I really don’t know what to dothisWill occur when the event is triggered.

Download mine for freeJavaScript beginner's manual

More browser tutorials: