Asynchronous loading JS (3)

Source: Internet
Author: User

1.6 Head.appendchild ()

To implement on-demand loading of the script and avoid blocking page rendering, you can use Head.appendchild () to

Add JavaScript scripts to the

var head = document.getElementsByTagName (' head ') [0],

Script = document.createelement (' script ');

script.src = URL;

Head.appendchild (script);

The disadvantage of Head.appendchild () is that users still need to process dependencies between modules before executing the code.

and load the dependent modules before executing the code.

1.7 Function Wrapping

In order to process the module dependencies and load ahead of time before the script executes, the user needs to use the function wrapper to build

Your own module-loading API.

Define (//The name of this module "Types/manager",//The array of dependencies["Types/employee"],//the function to execute When all dependencies//if all dependencies has loaded.//the arguments to this function is the array of//Dependenci Es mentioned above.function (Employee) {function Manager () {this.reports = [];} This would now Workmanager.prototype = new Employee ();//Return to the Manager constructor function//so it can is used by Other Modules.return Manager;});

The above is the syntax used by Requirejs, and it can be further simplified to load the original JavaScript file without having to define the module.

Require (["some/script.js"], function () {//This function was called after Some/script.js had loaded.});

As you can see, the syntax of the function wrapper is more concise and allows the loader to do a head.appendchile (script) load operation.

The normal CommonJS syntax differs from the syntax of the function wrapper, although they all work well in the browser.

If the server process can convert the function wrapper to a mode suitable for transport, then the CommonJS syntax can also be used in the Head.appendchild (script) type of the loading process.

However, it is more important not to force the runtime server process to convert the code, otherwise it can lead to debugging difficulties, and the function wrapper will lose line number information after injecting the server process.


Asynchronous loading JS (3)

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.