JS-封装 ajax XmlHttpRequest

来源:互联网 发布:淘宝店铺怎样装修视频 编辑:程序博客网 时间:2024/05/23 19:12
//定义一个对象 - 名字是$var $$ = function() {};//第二种写法$$.prototype = {    $id:function(id){        return document.getElementById(id)    },    $tag:function(tag) {        return document.getElementsByTagName(tag)    },    //ajax - 前面我们学习的    myAjax:function(URL,fn){        var xhr = createXHR();  //返回了一个对象,这个对象IE6兼容。        xhr.onreadystatechange = function(){            if(xhr.readyState === 4){                if(xhr.status >= 200 && xhr.status < 300 || xhr.status == 304){                    fn(xhr.responseText);                }else{                    alert("错误的文件!");                }            }        };        xhr.open("get",URL,true);        xhr.send();        //闭包形式,因为这个函数只服务于ajax函数,所以放在里面        function createXHR() {            //本函数来自于《JavaScript高级程序设计 第3版》第21章            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"                        ],                        i, len;                    for (i = 0, len = versions.length; i < len; i++) {                        try {                            new ActiveXObject(versions[i]);                            arguments.callee.activeXString = versions[i];                            break;                        } catch (ex) {                            //skip                        }                    }                }                return new ActiveXObject(arguments.callee.activeXString);            } else {                throw new Error("No XHR object available.");            }        }    },    //随机数    random: function (begin, end) {        return Math.floor(Math.random() * (end - begin)) + begin;    }}//在框架中实例化,这样外面使用的使用就不用实例化了$$ = new $$();