js onload Event | Run function on-page is loaded JavaScript

With the JS onload event, you can Execute JavaScript immediately after a page has been loaded. It works when on when an object has been loaded. onload is most often used within the element to execute a script once a web page is fully loaded.

Syntax

HTML

JavaScript

Examples of JS onload Event

Let’s see the example how to set event on HTML website using scripts.

HTML code– onload in body tag

Output:

JS on load event

JavaScript code– window.onload = codeAddress;

Output:

Examples of JS onload Event

How to run a function when the page is loaded?

Answer: There are 2 ways to to load script and function on HTML page.

Method 1: Using onload method:

Method 2: The window object represents the browser window.

Q: Where you can use JavaScript onload?

Answer: The onload event can be used to check the visitor’s browser type, browser version, notification, deal with cookies, etc, and load the proper version of the web page based on the information.

Do comment if you have any doubts and suggestion on this tutorial.

Note: The All JS Examples codes are tested on the Firefox browser (Version 12.0.2) and Chrome.
OS: macOS 10.14 Mojave
Code: HTML 5 Version


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.