4 Ways To Run Javascript After Page Load (Simple Examples)

Welcome to a quick tutorial on how to run Javascript after page load. Having some trouble with missing elements, variables, or functions? Want to delay a piece of Javascript until everything else is fully loaded?

The common ways to run Javascript after page load are:

  1. Add an event listener – document.addEventListener("load", FUNCTION);
  2. Add onload to the body tag – <body onload="FUNCTION()">
  3. Defer the script – <script src="SCRIPT.js" defer>
  4. Lastly, place the script at the very bottom of the page – Although this is not quite “after page load”.

That covers the basics, but let us walk through some detailed examples in this guide. Read on!

 

 

TABLE OF CONTENTS

 

 

EXECUTE ON PAGE LOAD

All right, let us now get into the ways and examples of how to run Javascript on page load.

 

1) EVENT LISTENER

1-event-listener.html
<!DOCTYPE html>
<html>
  <head>
    <title>Run Javascript On Page Load</title>
    <script>
    // (A) RUN THIS ON PAGE LOAD
    window.addEventListener("load", () => {
      document.getElementById("myTxt").style.color = "red";
    });
    </script>
  </head>
  <body>
    <!-- (B) HTML ELEMENTS -->
    <p id="myTxt">Hello world!</p>
  </body>
</html>

Events should be pretty self-explanatory, even if you have never heard of them before. They are fired whenever something of interest happens – Mouse click, scroll, keypress, video play, and more. In this example, we listen to the load event on the window itself; Run a piece of Javascript when the window is fully loaded.

P.S. You can also target the DOMContentLoaded event. The difference is that load is fired when everything is fully loaded. But DomContentLoaded is triggered as soon as all the HTML elements are ready. I.E. Images, CSS, videos may not be fully loaded.

 

2) BODY ON LOAD

2-onload.html
<!DOCTYPE html>
<html>
  <head>
    <title>Run Javascript On Page Load</title>
    <script>
    // (A) JAVASCRIPT FUNCTION
    function demo () {
      document.getElementById("myTxt").style.color = "green";
    }
    </script>
  </head>
 
  <!-- (B) RUN ON LOAD -->
  <body onload="demo();">
    <!-- (C) HTML ELEMENTS -->
    <p id="myTxt">Hello world!</p>
  </body>
</html>

Well, this kind of does the same thing as attaching addEventListener("load", FUNCTION). When the HTML document is loaded, onload="demo()" will be triggered.

 

 

3) DEFER SCRIPT LOAD

3a-defer.js
document.getElementById("myTxt").style.color = "blue";
3b-defer.html
<!DOCTYPE html>
<html>
  <head>
    <title>Run Javascript On Page Load</title>
    <!-- (A) DEFER SCRIPT LOAD -->
    <script src="3a-defer.js" defer></script>
  </head>
  <body>
    <!-- (B) HTML ELEMENTS -->
    <p id="myTxt">Hello world!</p>
  </body>
</html>

Yes, just add defer to the <script> tag, and it will hold until the entire page is done. For those who are confused about the difference between defer and onload:

  • defer will basically load the Javascript only when the HTML page is done.
  • onload does not mess with the loading order of the script, it simply runs the specified function when the page is ready.

P.S. Please take note that defer will not work with inline Javascript. I.E. Must load an external Javascript file.

 

4) SCRIPT AT BOTTOM (NOT QUITE AFTER PAGE LOAD)

4-bottom.html
<!DOCTYPE html>
<html>
  <head>
    <title>Run Javascript On Page Load</title>
  </head>
  <body>
    <!-- (A) HTML ELEMENTS -->
    <p id="myTxt">Hello world!</p>
 
    <!-- (B) SCRIPTS AT BOTTOM -->
    <script>
    document.getElementById("myTxt").style.color = "cyan";
    </script>
  </body>
</html>

Lastly, just put the scripts at the bottom of the page. This is not quite “after page load”, but it is… After everything else is nearly loaded.

 

 

DOWNLOAD & NOTES

Here is the download link to the example code, so you don’t have to copy-paste everything.

 

SORRY FOR THE ADS...

But someone has to pay the bills, and sponsors are paying for it. I insist on not turning Code Boxx into a "paid scripts" business, and I don't "block people with Adblock". Every little bit of support helps.

Buy Me A Coffee Code Boxx eBooks

 

EXAMPLE CODE DOWNLOAD

Click here to download. I have released it under the MIT license, so feel free to build on top of it or use it in your own project.

 

EXTRA BITS & LINKS

That’s all for this guide, and here is a small section on some extras and links that may be useful to you.

 

WHICH IS THE BEST METHOD?

Personally, I will usually use window.addEventListener("DOMContentLoaded", FUNCTION) or defer loading non-critical scripts. But whichever makes the most sense for your own project is the best method.

 

LINKS & REFERENCES

 

INFOGRAPHIC CHEAT SHEET

Run Javascript After Page Load (Click To Enlarge)

 

THE END

Thank you for reading, and we have come to the end of this guide. I hope that it has helped you with your project, and if you want to share anything with this guide, please feel free to comment below. Good luck and happy coding!