This article shares common methods for automatic execution of JS functions in web pages for your reference. The specific content is as follows
1. JS method
1. The simplest way to call it directly into the html body tag:
<body onload="myfunction()"> <html> <body onload="func1();func2();func3();"> </body> </html>
2. Call in JS statement:
<script type="text/javascript"> function myfun() { alert("this "); } /*Call myfun() with = myfun;//Don't bracket</script>
The third type
<script type="text/javascript"> =function(){ func1(); func2(); func3(); } </script>
2. JQ method
1. Execute all documents on the entire page after they are loaded. Unfortunately, this method not only requires that the page's DOM tree is loaded, but also requires that all external images and resources be loaded. What's more unfortunate is that if external resources, such as images, take a long time to load, then the execution of this js method feels slower. In other words, this is the most rigorous method of executing the method after the page is loaded.
=function() { $("table tr:nth-child(even)").addClass("even"); //This is jquery code };
2. Just load all DOM structures and execute the method before the browser puts all HTML into the DOM tree. Included before loading external images and resources.
$(document).ready(function() { $("table tr:nth-child(even)").addClass("even"); //Any js special effects that need to be executed });
There is also a way to abbreviate
$(function() { $("table tr:nth-child(even)").addClass("even"); //Any js special effects that need to be executed });
Three common methods are used to automatically execute JS functions in web pages
In the head area in HTML, there are the following functions:
<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 to automatically run when the web page is loaded!
①The first method
Change the above code to:
<SCRIPT LANGUAGE="JavaScript"> functionn MyAutoRun() { //The following is the code of your function, please modify it yourself first! alert("The function is executed automatically!"); } =MyAutoRun(); //Only this sentence is needed </SCRIPT>
②The second method
Modify the Body of the web page to:
<body onLoad="MyAutoRun();">
Or change to:
<body onLoad="javascript:MyAutoRun();">
③The third method
Use JS timer to execute functions intermittently:
setTimeout("MyAutoRun()",1000); //Execute the MyAutoRun() function once every 1000 milliseconds
To implement the method, change the top JS function to:
<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); //That's fine </SCRIPT>
Other methods are quite special, not commonly used, and not very versatile, so I won’t introduce them!
The above is a common method of automatic execution of JS functions, and I hope it will be helpful to your learning.