The OnLoad event, as the name implies, is for the JS program to execute after the Web page is loaded successfully. It is necessary for JS to be placed in the head.
How to bind the Web page onload, there are three main ways:
Window.onload=function () {//code to execute}
Window.addeventlistener (' Load ', function name, false)
Add Property <body onload= "function name ()" In Body >
<!DOCTYPE HTML Public "-//W3C//DTD XHTML 1.0 transitional//en" "http://www.w3.org/TR/xhtml1/DTD/ Xhtml1-transitional.dtd "><HTMLxmlns= "http://www.w3.org/1999/xhtml"><Head><Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8" /><title>Several ways to bind onload</title><Script>Alert ('I was first executed .');//This method can only execute a function after the page has been loaded (other functions are called through the function)window.onload=function() {alert (document.getElementById ('e'). getattribute ('ID'));}</Script><Script>if(document.all)//returns true in IEWindow.attachevent ('onload', add);//IE Browser using this methodElseWindow.addeventlistener ('Load', add,false);//Other browsers Use this methodif(document.all)//return false in non IEWindow.attachevent ('onload', Jian);ElseWindow.addeventlistener ('Load', Jian,false);//AddEventListener method to load event binding functionfunctionAdd () {alert ('the value of 1+2 is 3! ');}functionJian () {alert ('a value of 3-2 is 1! ');}functionCheng () {alert ('the value of 3*5 is 15! ');}//This function is called through the rows of the</Script></Head><Bodyonload= "Cheng ();"><!--after testing, in IE, the onload inside the body will be executed before the other load, and will overwrite the Window.onload method so that it cannot be executed; -<spanID= "E">Hehe</span><Script>if(document.all)//returns true in IEWindow.attachevent ('onload', last);//IE Browser using this methodElseWindow.addeventlistener ('Load', add,false);//Other browsers Use this methodfunctionLast () {alert ('I was finally executed!!! ');}</Script></Body></HTML>
How the OnLoad event is bound and executed in JavaScript