JavaScript non-blocking load script

Source: Internet
Author: User
Tags script tag

As more and more sites evolve into "Web 2.0″apps, the amount of JavaScript increases. This is a performance concern because scripts has a negative impact on page performance. Mainstream browsers (i.e., IE 6 and 7) block in both ways:

    • Resources in the page is blocked from downloading if they is below the script.
    • Elements is blocked from rendering if they is below the script.

As more and more Web sites are involved in "WEB 2.0" applications, the number of JavaScript scripts has also increased dramatically. This is worrying because the script has a negative impact on page performance. Mainstream browsers (IE 6, 7, for example) will block in the following 2 ways:

    • If the resources are located below the script, they will be blocked from downloading. Miyang County Marche Building Materials
    • If the elements are located below the script, they will be blocked from rendering the display.

The Scripts Block Downloads example demonstrates this. It contains the external scripts followed by an image, a stylesheet, and an IFRAME. The HTTP Waterfall chart from loading This example on IE7 shows that the first script blocks all downloads and then the Secon D script blocks all downloads, and finally the image, stylesheet, and iframe all download in parallel. Watching the page render, you'll notice that the paragraph of text above the script renders immediately. However, the rest of the text in the HTML document was blocked from rendering until all the scripts was done loading.

This script blocks the download example (Scripts block Downloads example) to show the above situation. It includes 2 external scripts, followed by 1 pictures, a style sheet, and an IFRAME. The HTTP waterfall diagram that uses IE7 to load This example shows that the first script blocks all downloads, and then the 2nd script blocks all downloads, and the last picture style sheet and iframe are loaded in parallel. Observe the rendering of the page, and you will see that the text above the script is immediately rendered. However, the text in the rest of the HTML document is blocked until all the scripts have been downloaded.

In IE6 7, Firefox 2 3.0, Safari 3, Chrome 1, Opera scripts will block the download

Browsers is threaded, so it's understandable, and a script is executing the browser was unable to start OT Her downloads. But there's no reason that while the script was downloading the browser can ' t start downloading other resources. And that's exactly what newer browsers, including Internet Explorer 8, Safari 4, and Chrome 2, has done. The HTTP Waterfall Chart for the Scripts Block Downloads example in IE8 shows the Scripts does indeed download in parallel, And the stylesheet is included in that parallel download. But the image and IFrame is still blocked. Safari 4 and Chrome 2 behave in a similar. Parallel downloading improves, but was still not as much as it could be.

If the browser is single-threaded, it is understandable that the browser cannot download other resources while a script is executing. But there is no reason why the browser cannot download other resources when the script is downloaded. Downloading scripts in parallel while downloading other resources is a feature that has been implemented for all the latest browsers, including Internet Explorer 8, Safari 4, Chrome 2. An HTTP waterfall diagram that opens a script blocking download in IE8 (Scripts block Downloads example) shows that the script is actually downloaded in parallel and the stylesheet is downloaded in parallel, but the picture and the IFrame are still blocked. The behavior of Safari 4 and Chrome 2 is similar. Parallel downloads have improved a bit, but have not yet achieved the best results it can achieve.

IE8, Safari 4, Chrome 2, scripts are still blocked

Fortunately, there is ways to get scripts to download without blocking any other resources on the page, even in older bro Wsers. Unfortunately, it's up to the web developer to do the heavy lifting.

Fortunately, even in older browsers, there are ways to keep scripts from blocking other page resources. Unfortunately, this depends on whether the web developer is willing to take on these heavy tasks.

There is six main techniques for downloading scripts without blocking:

    • XHR eval–download the script via XHR and Eval () the responsetext.
    • XHR injection–download the script via XHR and inject it into the page by creating a SCRIPT element and setting its text property to the ResponseText.
    • Script in Iframe–wrap your The script in a HTML page and download it as an Iframe.
    • Script DOM element–create a script Element and set its Src property to the script ' s URL.
    • Script Defer–add The script tag ' s Defer attribute. This used to only work in IE, and is now in Firefox 3.1.
    • document.write Script tag–write the <script src= "" > HTML into the page using document.write. This is loads script without blocking in IE.

There are 6 techniques for non-blocking loading scripts:

    • XHR Eval-executes through the XHR download script through eval ().
    • XHR Injection-through the XHR download script, the page is injected by creating a DOM element of the script, and the Text property is set.
    • script in Iframe-contains scripts within an HTML page and is loaded via an IFRAME.
    • Script DOM element-Creates a script's DOM elements and sets the SRC attribute to the URL of the scripts.
    • Script Defer-Increases the Defer property of the script. Once only available in IE, Firefox 3.1 now. can also be in the.
    • document.write Script Tag-use document.write <script src= "" > Inside HTML pages. Valid only in IE.

You can see a example of each technique using cuzillion. It turns out that these techniques has several important differences, as shown in the following table. Most of them provide parallel downloads, although script Defer and document.write script Tag is mixed. Some of the techniques can ' t be used on cross-site scripts, and Some require slight modifications to your existing scripts To get the them to work. An area of differentiation that's not widely discussed is whether the technique triggers the browser ' s busy Indicators (St ATUs Bar, progress bar, tab icon, and cursor). If you ' re loading multiple scripts this depend on each other, you'll need a technique that preserves execution order.

In cuzillion you can see examples of each technique. It turns out that these technologies have important differences, see the table below. Most of them offer parallel downloads, some techniques cannot be used on cross-domain scripts, some have to be easily modified based on the scripts you already have, and there is a non-extensive discussion of whether to trigger the browser's busy indicator differences (status bar, progress bar, tag icon, and cursor). If you need to load multiple interdependent scripts, you also need a technique for maintaining the order of execution.

?
Technical name Parallel Download Support support cross-domain no need to modify scripts do you have busy instructionsGuaranteed Execution Order size (bytes)
XHR Eval IE, FF, Saf, CHR, Op No No SAF, CHR - ~500
XHR Injection IE, FF, Saf, CHR, Op No Yes SAF, CHR - ~500
Script in Iframe IE, FF, Saf, CHR, Op No No IE, FF, Saf, CHR - ~50
Script DOM Element IE, FF, Saf, CHR, Op Yes Yes FF, SAF, CHR FF, Op ~200
Script Defer IE, SAF4, CHR2, FF3.1 Yes Yes IE, FF, Saf, CHR, Op IE, FF, Saf, CHR, Op ~50
document.write Script Tag IE, SAF4, CHR2, Op Yes Yes IE, FF, Saf, CHR, Op IE, FF, Saf, CHR, Op ~100

The question Is:which is the best technique? The optimal technique depends on your situation. This decision tree should is used as a guide. It ' s not as complex as it looks. Only three variables determine the Outcome:is the script in the same domain as the main page, is it necessary to preserve Execution order, and should the busy indicators be triggered.

The question is: which is the best technology? The best technology depends on your specific situation. The following decision tree can be used as a guide. It looks complicated, not really. Only 3 parameters determine the output: whether the script is under the same domain name on the main page, whether the execution order needs to be guaranteed, and whether the busy indicator needs to be triggered.

Ideally, the logic in this decision tree would is encapsulated in popular HTML templating languages (PHP, Python, Perl, et C.) So, the web developer could just call a function and being assured that their script gets loaded using the optimal te Chnique.

Ideally, the logic of this decision tree will be reflected in the popular HTML template language (PHP, Python, Perl, etc.), so web developers can simply call a function to ensure that their script uses the best techniques to load the script.

In many situations, the Script DOM Element is a good choice. It works in all browsers, doesn ' t has any cross-site scripting restrictions, are fairly simple to implement, and are well u Nderstood. The one catch is so it doesn ' t preserve execution order across all browsers. If you had multiple scripts that depend on each other, you'll need to concatenate them or use a different technique. If you have a inline script that depends on the external script, you'll need to synchronize them. I call the This "coupling" and present several ways to does this in coupling asynchronous Scripts.

In many cases, using the Script DOM element is a good choice. It works in all browsers without any cross-domain restrictions, is easily implemented on the code, is easy to understand, and the only thing that is not enough is to maintain the correct order of execution in all browsers. If you have multiple scripts and depend on each other, you need to merge them or use other technologies. If you have inline scripts that depend on external scripts within the page, you have to synchronize them, and I call this method "coupling" and suggest some ways to do this in a coupled asynchronous script.

JavaScript non-blocking load script

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.