space line
space line
space line
space line
space line
您现在的位置是: Javascript基础>>

oc版Ajax对象池

发布日期 2008-6-15 6:51:26
作者 shemily
出处 重用网
浏览次数 ...
今日浏览 ...
本月浏览 ...
我要留言 我要留言
space line
摘要
整理自prototype.js,其Ajax对象创建兼容目前大部分主流浏览器
space line
详细内容

具体代码

//------ajax and xmlHttpRequest obj pool---------
var Ajax = {
  obj: [],
  activeRequestCount: 0,

  getTransport: function() {
    return Try.these(
      function() {return new XMLHttpRequest()},
      function() {return new ActiveXObject("Msxml2.XMLHTTP")},
      function() {return new ActiveXObject("Microsoft.XMLHTTP")}
    ) || false;
  },

  getObj: function(){
 var i=0;
 for(i=0; i<this.obj.length; i++){
  if (this.obj[i].readyState == 0 || this.obj[i].readyState == 4){
                 return this.obj[i];
  }
 }

 this.obj[this.obj.length] = this.getTransport();
 return this.obj[this.obj.length - 1];
  }
}

var Try = {
  these: function() {
    var returnValue;

    for (var i = 0, length = arguments.length; i < length; i++) {
      var lambda = arguments[i];
      try {
        returnValue = lambda();
        break;
      } catch (e) {}
    }

    return returnValue;
  }
}//------ajax and xmlHttpRequest obj pool---------

调用方法

var myAjax = Ajax.getObj();

将上面代码部分复制到新建的JS文件中,在需要用到的页面引入该JS文件;

然后在需要用到ajax对象的地方,如上调用即可;

 

space line
我要留言 我要留言    
space line
space line
space line
space line
赞助商提供
space line
space line