JavaScript AJAX-based Inert Loading Function, javascriptajax

Source: Internet
Author: User

JavaScript AJAX-based Inert Loading Function, javascriptajax

Some memory in JS only needs to be executed once. For example, browser type detection is the most commonly used function, because we need to detect the built-in XHR of the browser when using Ajax. We can record the type during the first detection, and do not need to check the browser type when using Ajax later. In JS, even if there is only one if statement, it is always more efficient than the statement without if.

Common Ajax Method
Copy codeThe Code is as follows:
/**
* JS inertia Functions
*/
 
Function ajax (){
If (typeof XMLHttpRequest! = "Undefined "){
Return new XMLHttpRequest ();
} Else if (typeof ActiveXObject! = "Undefined "){
If (typeof arguments. callee. activeXString! = "String "){
Var versions = ["MSXML2.XMLHttp. 6.0", "MSXML2.XMLHttp. 3.0", "MSXML2.XMLHttp"];
 
For (var I = 0, k = version. length; I <k; I ++ ){
Try {
New ActiveXObject (versions [I]);
Arguments. callee. activeXString = versions [I];
Break;
} Catch (ex ){
Throw ex;
}
}
}
 
Return new ActiveXObject (arguments. callee. activeXString );
} Else {
Throw "No XHR object ";
}
}

Every time you call an ajax () function, you must check the browser's built-in XHR, Which is inefficient.

Method of Using inertia

Copy codeThe Code is as follows:
/**
* JS inertia Functions
*/
 
Function ajax (){
If (typeof XMLHttpRequest! = "Undefined "){
Ajax = function (){
Return new XMLHttpRequest ();
};
} Else if (typeof ActiveXObject! = "Undefined "){
Ajax = function (){
If (typeof arguments. callee. activeXString! = "String "){
Var versions = ["MSXML2.XMLHttp. 6.0", "MSXML2.XMLHttp. 3.0", "MSXML2.XMLHttp"];
 
For (var I = 0, k = version. length; I <k; I ++ ){
Try {
Var xhr = new ActiveXObject (versions [I]);
Arguments. callee. activeXString = versions [I];
Return xhr;
} Catch (ex ){
Throw ex;
}
}
}
 
Return new ActiveXObject (arguments. callee. activeXString );
}
} Else {
Ajax = function (){
Throw "No XHR object ";
}
}
 
Return ajax ();
}

In the second method, each branch of the if will assign a value to the ajax () variable, effectively overwriting the original function, and calling the new function in the last step. When ajax () is called next time, the variable is called directly.

Optimization focus

To execute specific code, only actual calls are required. Some JS libraries detect the browser at the beginning and are pre-configured.

Because of the complicated judgment, the first operation speed is slow, but the efficiency of multiple subsequent operations will be faster.
 
Sometimes the code has been written for a long time and cannot be changed. You must always think about how to make the program run faster and more efficient. The program written in this way is hard‑packed without generating additional junk code. This is not a simple way to achieve a one-size-fits-all. In fact, code is active in many places, and people are even more active.


Javascript ajax asynchronous transmission

Put action_ B In the callback of the success processing result returned by action_ B, and wait until the success completes processing and then calls

How does javaScript call ajax ??

Ajax is an interactive mode of js. We recommend that you first understand what ajax is. Through the httprequest (xmlrequest) object of js, read by Dynamic Language (asp, jsp, php ...) the generated static HTML page content (or xml, txt) is reflected by the data obtained from parsing the returned content.

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.