Home > Web Front-end > JS Tutorial > js lazy loading changes the position of JS to speed up web page loading

js lazy loading changes the position of JS to speed up web page loading

高洛峰
Release: 2017-01-04 10:42:29
Original
1172 people have browsed it

When a website has a lot of js code to be loaded, the location of the js code will affect the loading speed of the web page to a certain extent. In order to make our web page load faster, this article summarizes several points to note:
1. Delayed loading of js code

<script type=”text/javascript” src=”" id=”my”></script> 
<script type=”text/javascript”> 
setTimeout(“document.getElementById(‘my&#39;).src=&#39;include/php100.php&#39;; “,3000);//延时3秒 
</script>
Copy after login

This way, by delaying the loading of js code, more time is left for web page loading!

2. JS final loading plan 1
Insert the following code where JS needs to be inserted:
Program code

<span id=”L4EVER”>LOADING…</span>
Copy after login

Of course, that LOADING...you can change it to what you like Small picture. It looks very AJAX effect.
Then insert at the bottom of the page:
Program code

<span id=”AD_L4EVER”>你的JS代码在这里!</span > 
<script>L4EVER.innerHTML=AD_L4EVER.innerHTML;AD_L4EVER.innerHTML=”";</script>
Copy after login

3. Let JS load the last option 2
This involves the web page Loading order issues, for example, when introducing an external js script file, if it is placed in the head of the html, the js script will be loaded into the page before the page is loaded, and if it is placed in the body, it will be loaded from top to bottom according to the page. Run the javascript code sequentially~~~ So we can put the files imported from outside the js to the bottom of the page, so that the js can be imported last, thereby speeding up the page loading speed.


For more js lazy loading, change the position of JS to speed up web page loading. For related articles, please pay attention to 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