Three common methods for automatically executing JS functions on webpages
In the Head area of HTML, there are the following functions:
Copy codeThe Code is as follows:
<Script language = "JavaScript">
Functionn MyAutoRun ()
{
// The following is the code of your function. Please modify it first!
Alert ("automatic function execution! ");
}
</SCRIPT>
Next, let's take a look at the above functions to make them automatically run when loading webpages!
① Method 1
Change the code above:
Copy codeThe Code is as follows:
<Script language = "JavaScript">
Functionn MyAutoRun ()
{
// The following is the code of your function. Please modify it first!
Alert ("automatic function execution! ");
}
Window. onload = MyAutoRun (); // you only need to add this sentence
</SCRIPT>
② Method 2
Modify the webpage Body:
Copy codeThe Code is as follows:
<Body onLoad = "MyAutoRun ();">
Or change:
Copy codeThe Code is as follows:
<Body onLoad = "javascript: MyAutoRun ();">
③ Method 3
Use the JS timer to execute intermittent functions:
Copy codeThe Code is as follows:
SetTimeout ("MyAutoRun ()", 1000); // execute the MyAutoRun () function once every 1000 milliseconds
Implementation Method: Change the JS function at the top:
Copy codeThe Code is as follows:
<Script language = "JavaScript">
Functionn MyAutoRun ()
{
// The following is the code of your function. Please modify it first!
Alert ("automatic function execution! ");
}
SetTimeout ("MyAutoRun ()", 1000); // This way you can pull
</SCRIPT>
Other methods are special and not commonly used. They are not widely used!
EL & JSTL can also be used in JS.
Copy codeThe Code 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'/> ";