现在的位置: 首页 > 综合 > 正文

javascript Ajax And hashtable

2018年02月06日 ⁄ 综合 ⁄ 共 3191字 ⁄ 字号 评论关闭

function hashtable(){
       this.items = {}
}
var ht = new hashtable();
hashtable.prototype.Add = function(key,value) {
      if (typeof(key) == "undefined")
            return;
      if (ht.Contains(key)==true)
            return;
      if (typeof(value)=="undefined")
            return
      this.items[key] = value;
}

hashtable.prototype.Contains = function (key) {
        return typeof(this.items[key])!="undefined";
}

hashtable.prototype.Remove = function(key){
       delete this.items[key];
}

hashtable.prototype.Count = function (){
    var i=0;
    for (var k in this.items) i++;
    return i;
}

hashtable.prototype.item = function (key) {
       return this.items[key];
}

hashtable.prototype.Clear = function() {
      for (var k in this.items)
            delete this.items[k];
}

String.prototype.trim = function() {
     return this.replace(/(^/s*)|(/s*$)/g,'');
}

function checkByteLength(str,minlen,maxlen) {
 if (str == null) return false;
 var l = str.length;
 var blen = 0;
 for(i=0; i<l; i++) {
  if ((str.charCodeAt(i) & 0xff00) != 0) {
   blen ++;
  }
  blen ++;
 }
 if (blen > maxlen || blen < minlen) {
  return false;
 }
 return true;
}

 function loadCfr(selectN,n_selected,hast){
     for (var k in hast.items) {
                 selectN.options.add(new Option(k,hast.items[k]));
         }
     for (var i=0;i<selectN.options.length;i++) {
          if (i==n_selected)
               selectN.options[i].selected = true;
     }
 }
 
/*var zwd;
if (typeof zwd == 'undefined')
          zwd = {};
         
 zwd.Config = {
     Init:function(){
           alert(123);
         },
     Result:function(){
             alert(545);
        }
 }        
         
 */
   
 
     
var zwd = function() {}
 
zwd.prototype.Config = {
        Init:function(){
           alert(123);
         },
        Result:function(){
             alert(545);
        }
}

////////////////////Ajax////////////Class///////////////////
var Try = {
  these: function() {
    var returnValue;

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

    return returnValue;
  }
}

 function grr(rp) {
       if(RegExp.$1)/(.*)/.exec("");
       var re=new RegExp("<result>(.*)<//result>");
       re.exec(rp);
       if(RegExp.$1) return RegExp.$1;
       return "";
 }

var Ajax = function() {}
var xhr ;
Ajax.prototype.Init = function(){
      return Try.these(
          function() {return new ActiveXObject("Msxml2.XMLHTTP")},
          function() {return new ActiveXObject("Microsoft.XMLHTTP")},
          function() {return new XMLHttpRequest()}
      ) || false;
}

Ajax.prototype.Config = {
      Init:function() {},
      Result:""
}

var aj = new Ajax();

Ajax.prototype.func = function() {
            if (typeof xhr != 'undefined') 
                 if (xhr.readyState == 4) {
                        if (xhr.status == 200)
                          {
                                  aj.Config.Result = grr(xhr.responseText);
                                  //alert(aj.Config.Result);
                          }
                        else
                          {
                               aj.Config.Result = "";
                          }
              }
              else
              {
                  aj.Config.Result = "";
              }
    }
      

Ajax.prototype.Action = function(url) {
      xhr = aj.Init();  
      xhr.onreadystatechange = aj.func;
      xhr.open("GET",url);
      xhr.send(null);
}

Ajax.prototype.onResult = function(v) {
      return v==aj.Config.Result;
}

抱歉!评论已关闭.