Introduction to the use of JS:window.onload

  • 2020-03-29 23:45:09
  • OfStack

1, the simplest way to invoke
Write directly into the body tag of the HTML, such as:
 
<html> 
<body onload="func()"> 
</body> 
</html> 

2. Call in JS statement
 
<script type="text/javascript"> 
function func(){ ... } 
window.onload=func; 
</script> 

3. Call multiple functions at once
Write directly into the body tag of the HTML, such as:
 
<html> 
<body onload="func1();func2();func3();"> 
</body> 
</html> 

4, js call multiple functions, the following way of calling can be used in the less complex js program, if the program function is a lot of logic is complex, you can consider the fifth way.
 
<script type="text/javascript"> 
function func1(){ ... } 
function func2(){ ... } 
function func3(){ ... } 
window.onload=function(){ 
func1(); 
func2(); 
func3(); 
} 
</script> 

5. JS custom function is called many times
 
<script type="text/javascript"> 
function func1(){ ... } 
function func2(){ ... } 
function func3(){ ... } 
function addLoadEvent(func){ 
var oldonload=window.onload; 
if(typeof window.onload!="function"){ 
window.onload=func; 
} 
else{ 
window.onload=function(){ 
oldonload(); 
func(); 
} 
} 
} 
addLoadEvent(func1); 
addLoadEvent(func2); 
addLoadEvent(func3); 
</script> 

Related articles: