Delay loading of JavaScript code improves speed and delay of javascript

Source: Internet
Author: User

Delay loading of JavaScript code improves speed and delay of javascript

Delay loading of js Code improves the speed. The specific content is as follows:

If there is a large amount of javascript code in the webpage, it will greatly affect the webpage access speed. The following describes how to solve this problem.

1. Delayed loading of js files:

You can use the timer function setTimeout () to delay loading of external js files. 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 be loaded three seconds later, which can increase the speed to a certain extent.

2. Place the js Code behind the webpage content:

This is quite simple. I don't need to mention it. It is to put the js Code behind the content of the webpage, and put it at the bottom of the body. In this way, you can load the content before executing the js Code, it can also increase the speed.

The following describes how to accelerate loading of JavaScript ad code by loading the code in a delayed or final manner.

Delayed loading of JS ad code or final loading of accelerated page loading. If the page contains many JS ad code, or other js/'target = '_ blank' class = 'U'> JS Code is too large, so loading will take a lot of time. Use JS ad code to delay loading or final loading to speed up page loading.

First, JS delayed Loading

The Code is as follows:

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

Second, the JS advertisement code is finally loaded.

Insert the following code where Javascript is to be inserted:

Program code

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

<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 JS advertisement code to be displayed

<div id="guangg1"></div>

Ad content to be displayed

<div id="ggad1"><script language="javascript"src="/frontpage/include/php100.js"></script></div>

Determine whether the ad location to be displayed exists

<Script> function chkdiv (divid) {var chkid = document. getElementById (divid); if (chkid! = Null) {return true;} else {return false;} finally displays the JS advertisement code if (chkdiv ('guangg1 ') {document. getElementById ('guangg1 '). innerHTML = document. getElementById ('ggad1 '). innerHTML; document. getElementById ('ggad1 '). innerHTML = "" ;}</script>
Articles you may be interested in:
  • Batch Processing of JDBC statements improves processing speed
  • How to Improve the query statistics speed of MYSQL database select index Application
  • Summary of several methods to increase page loading speed
  • How does php process millions of databases and increase query processing speed?
  • Enable IIS6's GZIP function to speed up website opening and reduce bandwidth usage
  • Js uses image objects to pre-load images to increase access speed
  • ANT compression (remove spaces/comments) JS files can improve js Running Speed
  • How to use indexes to increase query speed
  • Loading Baidu sharing code with JS latency to improve webpage speed

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.