JS lazy load to speed up the page opening sample code


First, JS lazy loading

The code is as follows:


<script language="JavaScript" src="" id="my"></script>
<script language="JavaScript">
setTimeout("document.getElementById('my').src='include/... file ...php'; ",3000);//Delay 3 seconds
</script>

Second,JS loads last

Where JS needs to be inserted, insert the following code:

< SPAN id = L4EVER > LOADING… < / SPAN>

Sure, that LOADING… You can change it to your favorite small picture. It looks very AJAX.

Insert at the bottom of the page:


<SPAN class=spanclass id=AD_L4EVER>
 your JS Here's the code !</SPAN>
<script>L4EVER.innerHTML=AD_L4EVER.innerHTML;AD_L4EVER.innerHTML="";</script>

Third,JS loads last

Want to show the location of the AD < Div id = “guangg1” > < / div>

The content of the AD you want to display


<div id="ggad1" style="display:none">
<script language="javascript" src="/include/.......js"></script>
</div>

Determine whether the advertising location to be displayed exists


<script language="javascript">
function chkdiv(divid){
var chkid=document.getElementById(divid);
if(chkid != null)
{return true; }
else
{return false; }
}  Finally, display ads
if (chkdiv('guangg1')) {
document.getElementById('guangg1').innerHTML= document.getElementById('ggad1').innerHTML;
document.getElementById('ggad1').innerHTML="";
}
</script>

SetTimeout usage

The standard syntax for setTimeout is: setTimeout(expression, time (milliseconds)) with two parameters.

Note here that the first argument is a function call, assuming a function.

1. The function has no parameters:

The function alertV () {alert (” 000 ”); }

When the first parameter is not quoted ("" or ”), delay by one second: SetTimeout (alertV, 1000);

When the first parameter is quoted, delay by one second: SetTimeout (alertV ”()”, 1000);

2. Function has parameters:

Function alertV (event) {alert (” keyCode = ”+ event. KeyCode); }

This should be set to: SetTimeout (function () {alertV (event); }, 1000); Otherwise the parameter is not defined.