There are two ways you can wait for a page to load:

  • put your JavaScript code at the bottom of the page
  • use window.onload method

P.S. -> read this article if you want to double your salary as a programmer

Bottom Page JavaScript

If you put your JavaScript at the bottom things are very simple. Before your JS code is loaded, everything else had to be loaded first.

So this method doesn’t necessarily wait for anything. It just loads last so it achieves the same effect.

<html>
  <head>
    <title>Your Page</title>
  </head>
  <body>
    <!-- your page code here -->
    <script>
      // your JavaScript here
    </script>
  </body>
</html>

window.onload Method

When the page is finished loading, window.onload function is called. So by putting your custom code inside this method you can ensure your code runs only after the entire page was loaded.

<script>
  window.onload = function () {
    // your code here
  }
</script>

And this JavaScript code can be placed anywhere (beginning or end of the file). Because it will only run after the HTML file is completely loaded.

P.S. -> read this article if you want to double your salary as a programmer