Home  >  Article  >  Web Front-end  >  Several solutions to automatically execute (load) js on the page, detailed code analysis

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

亚连
亚连Original
2018-05-18 09:42:212302browse

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>

2. In JS Statement call:

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

3.

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

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代码 };

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特效 });

There is also an abbreviation method

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

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!

Statement:
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