Example code of JS delayed loading to speed up page opening

Source: Internet
Author: User

First, JS delayed Loading

The Code is as follows:
Copy codeThe Code is as follows:
<Script language = "JavaScript" src = "" id = "my"> </script>
<Script language = "JavaScript">
SetTimeout ("document. getElementById ('my'). src = 'include/... file... php';", 3000); // delay of 3 seconds
</Script>

Second, the final loading of JS

Insert the following code where Javascript is to be inserted:

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

Of course, the LOADING... You can switch to your favorite small image. It looks very AJAX-effective.

Insert at the bottom of the page:
Copy codeThe Code is as follows:
<SPAN class = spanclass id = AD_L4EVER>
Your JS Code is here! </SPAN>
<Script> L4EVER. innerHTML = AD_L4EVER.innerHTML; AD_L4EVER.innerHTML = ""; </script>

Third, JS final loading

Position of the advertisement to be displayed
<Div id = "guangg1"> </div>

Ad content to be displayed
Copy codeThe Code is as follows:
<Div id = "ggad1" style = "display: none">
<Script language = "javascript" src = "/include/...... js"> </script>
</Div>

Determine whether the ad location to be displayed exists
Copy codeThe Code is as follows:
<Script language = "javascript">
Function chkdiv (divid ){
Var chkid = document. getElementById (divid );
If (chkid! = Null)
{Return true ;}
Else
{Return false ;}
} Finally, the advertisement is displayed.
If (chkdiv ('guangg1 ')){
Document. getElementById ('guangg1 '). innerHTML = document. getElementById ('ggad1'). innerHTML;
Document. getElementById ('ggad1'). innerHTML = "";
}
</Script>

SetTimeout usage

The standard syntax of setTimeout is: setTimeout (expression, time (MS.

Note the call of the first parameter as a function. Assume It is a function.

1. The function has no parameters:

Function alertV () {alert ("000 ");}

When the first parameter is not enclosed by quotation marks ("" or ''), the delay is one second:
SetTimeout (alertV, 1000 );

When the first parameter is enclosed by quotation marks, the delay is one second:
SetTimeout ("alertvs ()", 1000 );

2. function parameters:

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

Set this parameter:
SetTimeout (function () {alertV (event) ;}, 1000); otherwise, the system will prompt that the parameter is not defined.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.