prototype_同步请求和回调函数

/*************** prototype 同步请求  ********************/
var Transfer={}
Transfer.Base = function() {}
Transfer.Base.prototype = {
  setOptions: function(options) {
    if(typeof options!="object"){options={};}
    this.options = {
        bCache:options.bCache||false,
        id:options.id||"scriptTemp",
        onfailure:options.onfailure||function(){},
        oncomplate:options.oncomplate||function(){}
    }
  }
}
var Class = {
  create: function() {
    return function() {
      this.initialize.apply(this, arguments);
    }
  }
}
Object.extend = function(destination, source) {
  for (var property in source) {
    destination[property] = source[property];
  }
  return destination;
}

/**
new Transfer.Request(url,{oncomplate:'',id:''})
*/
Transfer.Request=Class.create();
Transfer.Request.prototype=Object.extend(new Transfer.Base(),{
    initialize: function(url, options) {
    this.setOptions(options);
    this.request(url);
    },
    request:function(url){
        this.url=url;
        this.bCache=this.options.bCache;
        this.id=this.options.id;
        this.oncomplate=this.options.oncomplate;
        this.onfailure=this.options.onfailure;
        this.symbol="?";
        if(this.url.indexOf("?")!="-1")this.symbol="&";
        var head=document.getElementsByTagName("head")[0];
        var sT = document.getElementById(this.id);
        if(sT&&sT.src&&sT.src==this.url){
            this.oncomplate();
            return;
        }
        if (sT) {sT.parentNode.removeChild(sT);}
        var s = document.createElement("script");
        head.appendChild(s);
        s.setAttribute("language", "javascript");
    s.setAttribute("type", "text/javascript");
    s.setAttribute("id", this.id);
    s.setAttribute("src", (this.bCache && this.bCache == true) ? this.url + this.symbol + Math.random() : this.url);
        var self=this;
        s.οnlοad=s.onreadystatechange=function()
        {
            if (typeof ActiveXObject!="undefined") {
                if(s.readyState&&s.readyState=="loaded")self.oncomplate();
                if(s.readyState&&s.readyState=="complete")return;
            }else{
                self.oncomplate();
            }
        }
        s.οnerrοr=function(){ //ie not work
            s.parentNode.removeChild(s);
            self.onfailure();
            throw new Error("some error occurd,please try again later");
        }
     }
});
/********************************************/

调用方法:
new Transfer.Request("./decode.jsp?timestamp="+new Date().getTime()+""+parseInt(Math.random()*1000),{oncomplate:getLatlonCallback,id:'getLatlonCallback',bCache:true});

传入参数的方法:
new Transfer.Request(url,{oncomplate:function(){getAreaList(city[0]);},id:"areabycity_1",bCache:true});

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值