The following is a code example that I compiled for you to standardize the use of window.onload in js. Interested students can take a look.
1. Code that can be executed normally by window.onload
<html> <head> <script> window.onload=function() { alert(1); } </script> </head> <body> <div id="div">sdfasdf</div> </body> </html>
2. Code that cannot be executed normally
<html> <head> <script> window.onload=function() { alert(1); } function() { alert("z"); } </script> </head> <body onload="z()"> <div id="div">sdfasdf</div> </body> </html>
3. So where is the problem?
You can see that there is no js code except window.onload in the first piece of code, and there is an extra function z() in the second piece of code, and the second This piece of code specifies that the onload of the body is z(), so when the second piece of code is executed, it will only execute the js code in the z() function, and will not execute the js code in window.onload. Therefore, when you specify onload yourself, window.onload is invalid.
The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.
Related articles:
Explain to you in detail how to create div, span, label in js through code
How to create in js model data model (code provided, simple and easy to understand)
Native JS implementation method to determine collision (interesting example)
The above is the detailed content of Standard use of window.onload code example in js (including code). For more information, please follow other related articles on the PHP Chinese website!