JavaScript improves webpage loading speed

Source: Internet
Author: User

Introduction

Delaying the execution of JavaScript is a way to effectively improve the loading speed of web pages and Improve the Quality of user reading experience. From the actual experience, migrating our website from an affordable VPS to a Softlayer famous Data Center in the United States) the independent server platform can only increase the loading speed of the website by 20%, however, the delay in JavaScript execution can help increase the speed by 50%. Let's take a look at the statistics of the Google Webmaster tool> Site Performance website:

Practice

Web development follows the assumption that even if a JS file is suddenly interrupted, as long as there is no JS execution error, the web page will be correctly rendered. To put it simply, you can adopt the following two rules to delay the execution of javascript:

Wait until the page Document is ready and then execute the inline JS Code. These codes must be at least at the bottom of the page.

Dynamically load external JavaScript files. If multiple JS files are dependent on each other, make sure that the major JS file references are written at the bottom of the page for final loading.

The code snippet on the home page below shows how we can delay JavaScript execution during development.

 
 
  1. <script type="text/javascript">// <![CDATA[     
  2.          _lazyLoadScripts = new Array();    
  3.          _lazyExecutedCallbacks = new Array();     
  4.  // ]]></script>    
  5.  <script type="text/javascript" src="/scripts/jquery-1.4.4.min.js"></script>    
  6.  <script type="text/javascript" src="/scripts/website-lazy-load.js"></script>   

During development, some embedded web pages or components usually depend on some external JS files or JS code execution. In this case, you can define two variables "_ lazyLoadScripts" and "_ lazyExecutedCallbacks" at the top of the home page, as shown in the preceding example ".

In the following code snippet, you can see how these two variables are used on nested web pages or components.

 
 
  1. <script type="text/javascript">// <![CDATA[    
  2.      _lazyExecutedCallbacks.push(function ()    
  3.      {    
  4.          // in the case you need to execute some scripts in a nested page or module.    
  5.          // don't execute them explicitly, but push them into the callback list.    
  6.      });    
  7.  // ]]></script>    
  8.  <script type="text/javascript">// <![CDATA[    
  9.      // push the external JS files into the list for deferring loading.    
  10.      _lazyLoadScripts.push("/scripts/your-script.js");    
  11.  // ]]></script>  

These are pushed to) the JS Code in "_ lazyExecutedCallbacks" and the external JS files inserted into "_ lazyLoadScripts" will all be executed in the "website-lazy-load.js, the executed code snippets are as follows:

 
 
  1. // dynamically load external JS files when document ready    
  2. // dynamically load external JS files when document ready    
  3.  function loadScriptsAfterDocumentReady()    
  4.  {    
  5.      if (_lazyLoadScripts && _lazyLoadScripts != null)    
  6.      {    
  7.          for (var i = 0; i < _lazyLoadScripts.length; i++)    
  8.          {    
  9.              var scriptTag = document.createElement('script');    
  10.              scriptTag.type = 'text/javascript';    
  11.              scriptTag.src = _lazyLoadScripts[i];    
  12.              var firstScriptTag = document.getElementsByTagName('script')[0];    
  13.              firstScriptTag.parentNode.insertBefore(scriptTag, firstScriptTag);    
  14.          }    
  15.      }    
  16.  }    
  17.      
  18.  // Execute the callback when document ready.    
  19.  function invokeLazyExecutedCallbacks()    
  20.  {    
  21.      if (_lazyExecutedCallbacks && _lazyExecutedCallbacks.length > 0)    
  22.          for(var i=0; i<_lazyExecutedCallbacks.length; i++)    
  23.              _lazyExecutedCallbacks[i]();    
  24.  }    
  25.     
  26.  // execute all deferring JS when document is ready by using jQuery.    
  27.  jQuery(document).ready(function ()    
  28.  {    
  29.      loadScriptsAfterDocumentReady();    
  30.      invokeLazyExecutedCallbacks();    
  31.  });  

Tips

A reasonable step for developing a webpage should be to first compile HTML and CSS. After the pages are rendered correctly in the browser, you can write JavaScript code to support animations or other effects.

Do not write onclick = "..." code on any element on the HTML page to bind events, but bind them When HTML documents are ready. This avoids JS errors caused by The onclick event triggered by the user before the JS file is loaded.

If your website needs to load a wide range of external JS files, load them dynamically in the website-lazy-load.js, such as Google Analytics tracking JS files, Google AdSense JS files and so on.

This method is also effective for CSS files. But do not do this for the main CSS file.

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.