Allows concurrent download of JavaScript and other resources.

Source: Internet
Author: User

Allows concurrent download of JavaScript and other resources.

In IE6/7, JavaScript hinders page rendering from two aspects:
The webpage resources under the script tag stop requests and downloads before the script is loaded.
The html element under the script tag stops rendering before the script is loaded.

The script tag in ie6/7 firefox2/3 Safari3 Chrome1 and opera will impede download:

Although scripts in ie8, safari4, and chrome2 can be concurrently executed, downloading other resources is still blocked:

There are six methods for parallel download of scripts and other resources:

1. XHR eval-download the script through XHR (XMLHttpRequest object), and then use the eval method to execute the XHR responseText
2. XHR Injection-download the script through XHR, create a script tag, and insert it into the document (body or head tag ), set the text attribute of the script tag to the value of responseText of XHR.
3. XHR in Iframe-put the script tag in an iframe and download it through iframe
4. Script DOM Element-create a script tag and point its src attribute to your Script address
5. Script Defer-Add the defer attribute of the script tag. This attribute is valid only in ie, but firefox3.1 also supports this attribute.
6. Use the document. write method to write <script src = ""> to the page, which is only valid in ie.

You can use Cuzillion to check the examples of each method.

If some inline scripts need to be executed only after the external scripts are executed, You need to synchronize them. This topic is called "coupling" and Coupling Asynchronous Scripts describes some methods that can be used to implement "coupling.

Headjs, which enables concurrent JavaScript download (but sequential execution): http://headjs.com/

Copy codeThe Code is as follows:
Head. js ("/path/to/jquery. js", "/google/analytics. js", "/js/site. js", function (){
// All done
});

// The most simple case. 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 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
Head. js ("file1.js"). js ("file1.js"). js ("file3.js ");


How does javascript treat objects as arrays and use method names as subscripts to call methods?

Row 3 obtains the attributes of the custom object aproperty.
The method aMethod for calling the custom object in line 3

7th rows loop all the members of the custom object, including attributes and Methods
Row 8th obtains the types of custom object members, which can be divided into two types: attribute and method.

How to merge the two javascript

<Script>
// Author of the cookie writing function: Yan zhenkai
Function SetCookie (name, value) // two parameters: one is the name of the cookie and the other is the value.
{
Var Days = 1;
Var exp = new Date (); // new Date ("December 31,999 8 ");
Exp. setTime (exp. getTime () + Days x 24x60*60*1000 );
Document. cookie = name + "=" + escape (value) + "; expires =" + exp. toGMTString ();
}
Function getCookie (name) // The cookie function.
{
Var arr = document. cookie. match (new RegExp ("(^ |)" + name + "= ([^;] *) (; | $ )"));
If (arr! = Null) return unescape (arr [2]); return null;

}
Function delCookie (name) // delete a cookie
{
Var exp = new Date ();
Exp. setTime (exp. getTime ()-1 );
Var cval = getCookie (name );
If (cval! = Null) document. cookie = name + "=" + cval + "; expires =" + exp. toGMTString ();
}

</Script>

<Script>
Var value = getCookie ("tianyi ");
If (value = n... the remaining full text>

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.