We often use window. onload to process pages. When page loading is complete, we will do something. But this window. onload is the completion of page loading, even including images. Next we will discuss in detail the window. onload usage the javascript script code in the webpage can be executed only after the document is loaded. Otherwise, the object cannot be obtained. To avoid this situation, you can use either of the following methods:
1. Place the script code at the low end of the Web page. when running the script code, you can ensure that the object to be operated has been loaded.
2. Run the script code through window. onload.
The first method is messy (recommended in fact). We often need to place the script code in a more appropriate place, so the window. onload method is a better choice. Window. onload is an event that is triggered after the document is loaded. You can register the event processing function for this event and place the script code to be executed in the event processing function, therefore, you can avoid getting objects. Let's take a look at a code example:
Window. onload usage