Three common methods are used to automatically execute JS functions in web pages
In the head area in HTML, there are the following functions:
The code copy is as follows:
<SCRIPT LANGUAGE="JavaScript">
functionn MyAutoRun()
{
//The following is the code of your function, please modify it yourself first!
alert("The function is executed automatically!");
}
</SCRIPT>
Next, we will target the above function and let it run automatically when the web page is loaded!
①The first method
Change the above code to:
The code copy is as follows:
<SCRIPT LANGUAGE="JavaScript">
functionn MyAutoRun()
{
//The following is the code of your function, please modify it yourself first!
alert("The function is executed automatically!");
}
window.onload=MyAutoRun(); //Only this sentence needs to be added
</SCRIPT>
②The second method
Modify the Body of the web page to:
The code copy is as follows:
<body onLoad="MyAutoRun();">
Or change to:
The code copy is as follows:
<body onLoad="javascript:MyAutoRun();">
③The third method
Use JS timer to execute functions intermittently:
The code copy is as follows:
setTimeout("MyAutoRun()",1000); //Execute the MyAutoRun() function once every 1000 milliseconds
To implement the method, change the top JS function to:
The code copy is as follows:
<SCRIPT LANGUAGE="JavaScript">
functionn MyAutoRun()
{
//The following is the code of your function, please modify it yourself first!
alert("The function is executed automatically!");
}
setTimeout("MyAutoRun()",1000); //This is all right
</SCRIPT>
Other methods are quite special, not commonly used, and not very versatile, so I won’t introduce them!
EL& JSTL can also be used in JS
The code copy is as follows:
var step = "<c:out value='${step}' default='0'/>";
switch(step) {
case"0":
default:
break; var step = "<c:out value='${step}' default='0'/>";