window.onload()The method is used to execute an operation immediately after the webpage is loaded; that is, once the HTML document has finished loading, a method is executed immediately.

window.onload()Usually used on the <body> element to execute script code after the page has completely loaded (including images, CSS files, etc.).

Syntax when there is only one function to execute:

window.onload = funcRef;

The funcRef method will be called after the page has finished loading.

Syntax when there are multiple functions to execute:

window.onload=function(){
    Func1();
    Func2();
    Func3();
    .....
}

After the page has finished loading, Func1, Func2, and Func3 will be executed in order.


Why use window.onload()?

Because JavaScript function methods can only be used after the HTML document has finished rendering. If rendering is not complete, the DOM tree is incomplete at that point, so calling some JavaScript code may result in an "undefined" error.

Example

<!DOCTYPE html> <html> <head> <meta charset=" utf-8"> <title>Test without using window.onload()</title> <style type="text/css"> #bg{ width:120px; height:120px; border:4px solid blue; } </style> <script type="text/javascript"> document.getElementById("bg").style.backgroundColor="#F00"; </script> </head> <body> <div id="bg"></div> </body> </html>

Try it yourself »

In the above example, the effect we want to achieve is to set the background color of the div to #F90, but this effect was not achieved because the code is executed sequentially. When execution reachesdocument.getElementById("#bg").style.backgroundColor="#F00"the div object has not yet been loaded, so the background color was not set successfully. The error message is as follows:

We can add window.onload and it will execute normally. The modified code is as follows:

Example

<!DOCTYPE html> <html> <head> <meta charset=" utf-8"> <title>Test using window.onload()</title> <style type="text/css"> #bg{ width:120px; height:120px; border:4px solid blue; } </style> <script type="text/javascript"> window.onload=function(){ document.getElementById("bg").style.backgroundColor="#F00"; } </script> </head> <body> <div id="bg"></div> </body> </html>

Try it yourself »

The window.onload event binds an event handler function. It binds an anonymous function, and of course you can also bind a named function. The code example is as follows:

Example

<!DOCTYPE html> <html> <head> <meta charset=" utf-8"> <title>Using window.onload() to bind a specific function</title> <script type="text/javascript">// Function Nameis example window.onload=function example(){ document.write(“Rookie Tutorial -- Learning is not only about technology, but also about dreams!!!"); }</script> </head> <body> </body> </html>

Try it yourself »

Example with multiple functions to execute:

Example

<!DOCTYPE html> <html> <head> <meta charset=" utf-8"> <title>Using window.onload() to execute multiple functions</title> <style type="text/css"> #bg{ width:100px; height:100px; border:2px solid blue; } </style> <script type="text/javascript"> window.onload=function(){ function example1(){ document.getElementById("bg").style.backgroundColor="#F00"; } function example2(){ document.getElementById("bg").style.width="200px"; document.getElementById("bg").style.height="200px"; } example1(); example2(); } </script> </head> <body> <div id="bg"></div> </body> </html>

Try it yourself »

The difference between window.onload and jQuery ready()

window.onload = function () {};    // JavaScript 
$(document).ready(function () {}); // jQuery

Both of the above methods execute DOM operations after the HTML document has finished loading, but they still have certain differences, as shown in the figure below: