Home > Web Front-end > JS Tutorial > body text

About the XMLHttpRequest object

一个新手
Release: 2017-09-20 10:09:30
Original
1397 people have browsed it


要使用XHR对象,需要编写一个函数。
Copy after login

Method 1:

The advantage is simple and it can run on most IE versions. However, there is no version detection, so I don’t know if it will work on lower versions.

var http_request; 
function creatXHR(){
            if(window.ActiveXObject){
                http_request=new ActiveXObject("Microsoft.XMLHTTP");
            }else{
                http_request=new XMLHttpRequest();
            }
}
Copy after login

Method Two

来自书《JavaScript高级程序设计(第3版)》
首先检测原生XHR对象是否存在,如果存在就返回它的新实例。如果不存在就检测ActiveX对象。如果两种都不存在,就抛出错误。
关注if (typeof arguments.callee.activeXString != "string")这一句,如果有以前缓存的activeXString对象,下一次就不会执行if里面的东西。通过数组和for循环创建,很清晰,很严谨。
Copy after login
function createXHR() {
        if (typeof XMLHttpRequest != "undefined") {            
        return new XMLHttpRequest(); //IE7+和其他浏览器支持的
        } else if (typeof ActiveXObject != "undefined") { //IE7-支持的
            **if (typeof arguments.callee.activeXString != "string")** {                
            var versions = ["MSXML2.XMLHttp.6.0", "MSXML2.XMLHttp.3.0", "MSXML2.XMLHttp"],
                    i, len;                
                    for (i = 0, len = versions.length; i < len; i++) {                    
                    try {                        
                    new ActiveXObject(versions[i]);                        
                    arguments.callee.activeXString = versions[i];                        
                    break;
                    } catch (e) {                        //跳过
                    }
                }
            }            return new ActiveXObject(arguments.callee.activeXString); //返回ActiveXObject对象
        } else { //全部不支持,抛出错误
            throw new Error("No XHR object available!");
        }
    }    var XHR = createXHR();
Copy after login

Method Three

网上看见的方法,考虑惰性函数的优点,一次检测之后重写构造方法。
在方法二的基础上加入惰性函数,构成方法三。
Copy after login

   **惰性载入表示函数执行的分支只会在函数第一次掉用的时候执行,在第一次调用过程中,该函数会被覆盖为另一个按照合适方式执行的函数,这样任何对原函数的调用就不用再经过执行的分支了。**
Copy after login

   哇!觉得惰性函数是真的好玩!
Copy after login
function createXHR() {
        if (typeof XMLHttpRequest != "undefined") {            //在第一次执行的时候重写createXHR函数
            createXHR = function() {
                return new XMLHttpRequest();
            };

        } else if (typeof ActiveXObject != "undefined") {

            createXHR = function() {
                if (arguments.callee.activeXString != "string") {                    
                var versions = ["MSXML2.XMLHttp.6.0", "MSXML2.XMLHttp.3.0", "MSXML2.XMLHttp"],
                        i, len;                    
                        for (i = 0, len = versions.length; i < len; i++) {                        
                        try {                            
                        new ActiveXObject(versions[i]);                            
                        arguments.callee.activeXString = versions[i];                            
                        break;
                        } catch (ex) {                            //跳过
                        }
                    }

                }                
                return new ActiveXObject(arguments.callee.activeXString);
            };

        } else {

            createXHR = function() {
                throw new Error("No XHR object available.");
            };

        }        
        return createXHR();
    }    
    var XHR = createXHR();
Copy after login

The above is the detailed content of About the XMLHttpRequest object. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template