Home > Article > Web Front-end > js lazy loading changes the position of JS to speed up web page loading
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').src='include/php100.php'; “,3000);//延时3秒 </script>
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>
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>
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!