How to add a window.onload event handler with JavaScript?

Sometimes, we want to add a window.onload event handler with JavaScript.

In this article, we’ll look at how to add a window.onload event handler with JavaScript.

How to add a window.onload event handler with JavaScript?

To add a window.onload event handler with JavaScript, we call window.addEventListener.

For instance, we write

window.addEventListener("load", () => {
  //...
});

to call window.addEventListener with 'load' to add a load event handler.

Conclusion

To add a window.onload event handler with JavaScript, we call window.addEventListener.