Home > Web Front-end > JS Tutorial > body text

Several solutions to automatically execute (load) js on the page, detailed code analysis

亚连
Release: 2018-05-18 09:42:21
Original
2363 people have browsed it

Below are several solutions I have compiled for you to automatically execute (load) js on the page. Interested students can take a look.

1. JS method

1. The simplest calling method is to write directly into the body tag of html:

<body onload="myfunction()">
<html> <body onload="func1();func2();func3();"> </body> </html>
Copy after login

2. In JS Statement call:

<script type="text/javascript">
  function myfun()   
{    alert("this window.onload");   }   /*用window.onload调用myfun()*/  
window.onload = myfun;//不要括号
</script>
Copy after login

3.

<script type="text/javascript">
window.onload=function(){
func1();
func2();
func3(); }
</script>
Copy after login

2. JQ method

1. Execute after all documents of the entire page are loaded. Unfortunately, this method not only requires that the DOM tree of the page be fully loaded, but also requires that all external images and resources be loaded. What's even more unfortunate is that if external resources, such as images, take a long time to load, then the execution of this js method will feel slower. In other words, this is the most rigorous method of executing the method after the page is loaded.

window.onload =function() { $("table tr:nth-child(even)").addClass("even"); //这个是jquery代码 };
Copy after login

2. Just load all the DOM structures and execute the method before the browser puts all the HTML into the DOM tree. Included before loading external images and resources.

$(document).ready(function() { $("table tr:nth-child(even)").addClass("even"); //任何需要执行的js特效 });
Copy after login

There is also an abbreviation method

$(function() { $("table tr:nth-child(even)").addClass("even"); //任何需要执行的js特效 });
Copy after login

The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.

Related articles:

Use js to determine whether the client can access the Internet (code attached)

Details for you Analyze the use of AJAX (code pasted)

Interview questions about AJAX (with answers)

The above is the detailed content of Several solutions to automatically execute (load) js on the page, detailed code analysis. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template