Delay loading JavaScript code improve speed _javascript Skills

Source: Internet
Author: User

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>
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.