Async attributes of scripts

Source: Internet
Author: User

The script tag has a very uncommon async attribute, which was first created by Firefox and then supported by chrome and Safari, but unfortunately IE does not support this attribute.

For introduction to async, see here: http://www.w3.org/TR/html5/scripting-1.html#attr-script-async

We can understand the async attributes as follows:

When the async attribute of a script is set to true, the execution sequence of the script is asynchronous. That is, it is not executed in the order of mounting to the Dom. If it is false, it is executed in the order of mounting.

If the script tag is directly encoded in HTML, The async attribute recognized by the handler is false. If the script is obtained from document. createelement ("script"), The async attribute is true.

IE, Safari, and opera are completely irrelevant. If you dynamically create a script tag, opera will execute the script in the order it is mounted to the Dom, however, the asynchronous execution of IE and Safari is not powerful at this time.

There is a problem here. We have a need to dynamically load JavaScript, and it is possible to dynamically load one or more JavaScript scripts at any time, in addition, this load must be cross-origin. Xhr injection (loading JavaScript code dynamically) cannot be considered. Then how can we perform sequential download and execution?

We can try this method. First, we use a script tag of type = "text/SEED" to download all the scripts, you can also set it to whatever text/someelse you want to set. Of course, JavaScript cannot be written. After all the scripts are downloaded, the script tag is re-generated in sequence and mounted to the DOM tree.

This is equivalent to using the browser cache to pre-load the file and then execute it one by one. However, this solution has no theoretical basis, because I don't know how the browser uses the cache. However, all the tested browsers have achieved the expected results.

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.