This article introduces how to delay the loading of javascript code and speed up the access of web pages.
When a website has a lot of js code to load, the location of js code will image the loading speed of web pages to a certain extent. In order to make our web pages load faster, this article summarizes several points of attention:
1. Delay loading js code
Copy the codeThe code is as follows:
<script type=”text/javascript” src=”" id=”my”></script>
<script type=”text/javascript”>
setTimeout("('my').src='include/'; ",3000);//Delay for 3 seconds
</script>
In this way, the js code is loaded by delaying the loading of the web page!
2. js last loading plan one
Insert the following code where you need to insert JS:
Program code
Copy the codeThe code is as follows:
<span id=”L4EVER”>LOADING…</span>
Of course, that LOADING...you can change it to a small picture you like. It looks very AJAX effect.
Then insert at the bottom of the page:
Program code
Copy the codeThe code is as follows:
<span id="AD_L4EVER">Your JS code is here!</span>
<script>=AD_L4EVER.innerHTML;AD_L4EVER.innerHTML=”";</script>
3. Let JS finally load solution two
This involves the loading order of web pages. 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. When put into the body, the javascript code will be run in the order of loading the page from the top~~~ So we can put the file introduced outside js at the bottom of the page to allow js to be introduced at the end, thereby speeding up the page loading speed.