Ways to allow JavaScript and other resources to be downloaded concurrently _javascript tips

Source: Internet
Author: User
Tags script tag

In Ie6/7, JavaScript blocks page rendering in two ways:
The page resources below the Script tab stop the request and download before the script is loaded.
The HTML element underneath the script tag stops rendering before the script finishes loading.

The script tags under IE6/7 firefox2/3 Safari3 Chrome1 and opera block downloads:

Although the script can be concurrent under ie8,safari4,chrome2, it still hinders the downloading of other resources:

There are 6 ways to enable script to be downloaded in parallel with other resources:

1.XHR eval-The script through the XHR (XMLHttpRequest object) and executes XHR responsetext with the Eval method
2.XHR injection-Downloads the script by XHR, then creates a script tag and inserts it into the document (body or head tag), and then sets the script label's Text property to the XHR responsetext value
3.XHR in iframe-put the script tag in an iframe and download it through the IFRAME
4.Script DOM element-creates a script tag and points its SRC attribute to your script address
5.Script defer-adds the Defer property of the Script tag, which is only valid in IE, but firefox3.1 also supports this property.
6. Use the Document.Write method to write <script src= "" In the page, this only works in IE

You can see examples of how each method is used through cuzillion.

If there are any inline scripts that need to be executed after the external script executes, then they need to be synchronized (synchronize). Called "Coupling," coupling asynchronous Scripts This article describes some of the ways in which "coupling" can now be implemented.

Headjs, can make JS concurrent download (but sequential execution): http://headjs.com/

Copy Code code as follows:

Head.js ("/path/to/jquery.js", "/google/analytics.js", "/js/site.js", function () {
All done
});

The most simple case. The load and execute single script without blocking.
Head.js ("/path/to/file.js");

Load a script and execute a function after it has been loaded
Head.js ("/path/to/file.js", function () {

});

Load files in parallel but execute them in sequence
Head.js ("File1.js", "file2.js", ...) "Filen.js");

Execute function after the all scripts have been loaded
Head.js ("File1.js", "File2.js", function () {

});

Files are loaded in parallel and executed in order they arrive
Head.js ("File1.js");
Head.js ("File2.js");
Head.js ("File3.js");

The previous can also be written as
Head.js ("File1.js"). JS ("File1.js"). JS ("File3.js");

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.