Delay loading JS code to improve speed, the specific contents are as follows:
If there is a lot of JavaScript code in the Web page that can greatly affect the speed of web pages, here's a quick way to deal with this problem.
A. Delay Loading JS file:
You can use the Timer function settimeout () to delay the loading of the external JS file, for example:
<script type= "Text/javascript" src= "id=" my "></script>
<script type=" Text/javascript ">
var Js=document.getelementbyid ("my");
function Addjs () {
js.src= "js/js.js";
}
SetTimeout (addjs,3000);
</script>
The above code can implement the JS code 3 seconds before loading, which can improve the speed to some extent.
Two. Place the JS code behind the content of the Web page:
This is simpler, needless to say, is the JS code placed in the back of the content of the Web page, one is placed at the bottom of the body, so you can load the content before the implementation of JS code, but also to improve a certain speed.
Here to introduce the JS AD code delay load or last load accelerated page load
JS AD code delay loading or finally load accelerated page load, if the page has a lot of JS advertising code, or other js/' target= ' _blank ' class= ' u ' >js code too large, this time loading up will be very time-consuming. Use the JS ad code to delay loading or finally load the method to speed up the page load.
First, JS delay loading
The code is as follows:
<script language= "JavaScript" src= "id=" my "></script>
<script>
settimeout (" document.getElementById (' my '). src= ' include/php100.php '; ", 3000)//Delay 3 sec
</script>
Second, JS advertising code finally loaded
Insert the following code where you want to insert JS:
Program code
<span id=l4ever>loading...</span>
Of course, that loading ... You can switch to your favorite small picture. It looks like an Ajax effect.
Then insert at the bottom of the page:
<span class=spanclass id=ad_l4ever>
your JS code here!</span>
<script>l4ever.innerhtml=ad_ l4ever.innerhtml; Ad_l4ever.innerhtml= "";</script>
Third, JS finally loaded
To show the location of the JS AD code
<div id= "GUANGG1" ></div>
The content of the advertisement you want to display
<div id= "Ggad1" ><script language= "javascript"
src= "/frontpage/include/php100.js" ></script> </div>
Determine if there is an ad location to display
<script>
function Chkdiv (divid) {
var chkid=document.getelementbyid (divid);
if (chkid!= null)
{return true;}
Else
{return false;}
} The last is to display the JS AD code
if (Chkdiv (' Guangg1 ')) {
document.getElementById (' Guangg1 '). innerhtml= document.getElementById (' Ggad1 '). InnerHTML;
document.getElementById (' Ggad1 '). innerhtml= "";
}
</script>