Page Optimization--js Asynchronous loading

Source: Internet
Author: User

Synchronous Loading

Before the introduction of JS asynchronous loading. Let's take a look at what JS synchronous loading is. What we usually use most often is this synchronous loading form:

<script src= "Http://XXX.com/script.js" ></script>

Synchronization mode. Also known as blocking mode, will prevent the browser may be processing, stop the resolution. As a result, the file loading (like), rendering, and code execution are stopped. A Generic Script tag (without an async attribute) can clog the browser when it is loaded, that is, when the browser downloads or runs the JS block of code. After the label will not be parsed, such as in the head to add a script, but this script download network instability, very long time did not download the corresponding JS file, then the browser has been waiting for this JS file download, the page will not be rendered, the user sees is white screen.

Once the general recommendation is to put <script> at the end of the page </body> before, so as to minimize such blocking behavior. And let the page show up first.

Asynchronous loading

It agrees with the non-clogging resource loading. And make the onload start faster, agreeing to load the page content. Instead of having to refresh the page, you can delay loading dependencies based on the content of the page.

Common Asynchronous loading examples:

(Script DOM Element)

(function () {     var s = document.createelement (' script ');     S.type = ' text/javascript ';     S.async = true;     S.SRC = ' http://yourdomain.com/script.js ';     var x = document.getelementsbytagname (' script ') [0];     X.parentnode.insertbefore (S, x); })();

The way to do this is to create a SCRIPT element and insert it into the document using JS in the <script> tab of the page.

This will do a non-clogging download JS code.

Delay Loading(lazy Loading)The asynchronous load (async loading) problem was overcome before we talked about what deferred loading is.
Delayed loading: Some JS code is not required when the page is initialized, and some of the circumstances are needed later. Delay loading is the beginning does not load these temporary unused JS, but at the time of need or later through the control of JS to asynchronously loaded.


That is, JS cut into a number of modules, the page is initialized to only load the JS that need to run immediately, and then other JS loading delay until the first time needed to load.
In particular, the page has a large number of different modules, very much may be temporary or no use at all.


Just like the delayed loading of a picture, the display image is loaded when the picture is in the visible area (dropdown in the scroll bar).


Asynchronous loading. It is necessary to divide the whole JS content into a modular way, in which there is a dependency, and asynchronous loading does not guarantee the order of operation. JS Modularity overcomes this problem, please? Understand modular development ?

?

Page Optimization--js Asynchronous loading

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.