How to run code after the page has loaded completely with JavaScript?

Sometimes, we want to run code after the page has loaded completely with JavaScript.

In this article, we’ll look at how to run code after the page has loaded completely with JavaScript.

How to run code after the page has loaded completely with JavaScript?

To run code after the page has loaded completely with JavaScript, we can run it in the window.onload method.

For instance, we write:

window.onload = () => {
  console.log('page loaded')
}

to set the window.onload property to a function that runs when the page is completely loaded.

Therefore, we see 'page loaded' logged when the page is loaded completely.

Conclusion

To run code after the page has loaded completely with JavaScript, we can run it in the window.onload method.