从新浪网搜刮的js函数和类

转载:http://www.verydemo.com/demo_c98_i48246.html

/**
 * 去除字符串前后的空格
 * 使用方法:   str.trim();
 * @return string
 */
String.prototype.trim = function()
{
// 用正则表达式将前后空格
// 用空字符串替代。
 return this.replace(/(^/s*)|(/s*$)| /g, "");
}

/**
 * 精确统计字符串字节数
 *
 * return integer
 */
String.prototype.ByteCount = function(trim)
{
 txt = this.replace(/(<.*?>)/ig,'');
 if(trim == true)txt = this.replace(/[/s/t/r/n]/g, '');
 txt = txt.replace(/([/u0391-/uFFE5])/ig, '11');
 var count = txt.length;
 return count;
}

//双字节字符截取
String.prototype.cn_substr = function(start, len, replace)
{
 var s = this.replace(/([/u4e00-/u9fa5])/g,"/xff/$1");
 if(s.length > len)
 {
  if(s.length == this.length)return this.substring(start, len);
  return (s.substring(start, len).replace(//xff/g, '') + replace);
 }
 return this;
 //return s.substring(start, len).replace(//xff/g, '');
}

var p$ = function(objName)
{
 if(parent.document.getElementById)
 {
  return eval('parent.document.getElementById("'+objName+'")');
 }
 else
 {
  return eval('parent.document.all.'+objName);
 }
}
function $id(id)
{
 return document.getElementById(id);
}

function $tag(name, obj)
{
 if(typeof(obj) == "undefined")
 {
  obj = document;
 }
 return obj.getElementsByTagName(name);
}

function getFrameNode(sNode)
{
 return document.frames ? document.frames[sNode] : document.getElementById(sNode).contentWindow;
}

....................................................................

function is_mod(num, mod_num)
{
 if((num % mod_num) == 0)
 {
  return true;
 }
 return false;
}

function is_float(str, nozero)
{
 if(!nozero)
 {
  var reg = /^[0-9]/d*(./d+)?$/;
 }
 else
 {
  var reg = /^[1-9]/d*(./d+)?$/;
 }
 return reg.test(str);
}

function str_line(str, len)//超长连续字符按长度在后面加空格,防止撑开页面
{
 var l = str.ByteCount();
 if(l <= len)
 {
  return str;
 }
 var s = new Array();
 while(l > len)
 {
  var tmp = str.cn_substr(0, len, "");
  s.push(tmp);
  var n = tmp.ByteCount();
  str = str.cn_substr(n, l, "");
  l = l - n;
 }
 s.push(str);
 str = s.join(" ");
 return str;
}

function create_js(id, src)
{
 var js = document.getElementById(id);
 if (js != null && js.tagName.toLowerCase() == "script")
 {
  document.getElementsByTagName("head").item(0).removeChild(js);
 }
 src += "&_t=" + new Date().getTime();
 js = document.createElement('script');
 js.id = id;
 js.src = src;
 document.getElementsByTagName("head").item(0).appendChild(js);
}

function checkCount(name, checked, obj)
{
 var e = $tag("INPUT", obj);
 var count = 0;
 for(var i = 0; i < e.length; i++)
 {
  if(e[i].name == name && e[i].checked == checked)
  {
   count++;
  }
 }
 return count;
}

function class_ajax()
{
 this.ID = "ajax";
 /**
  * xml请求对象
  */
 this.xmlHttp = null;
 /*
  * 请求的目标url
  */
 this.url = "";
 /**
  * http请求的方法 GET or POST
  */
 this.method = "";
 /**
  * 状态改变的事件触发函数
  */
 this.event = "";
 /**
  * 是否为异步连接
  */
 this.async = true;
 /**
  * REQUEST参数
  */
 this.params = new Array;
 
 
 /**
  * 初始化xmlHttpRequest对象
  *
  */
 this.init = function()
 {
  if(this.xmlHttp == null)
  {
   if (window.XMLHttpRequest)
      {
          this.xmlHttp = new XMLHttpRequest();
      }
       else if (window.ActiveXObject)
       {
          this.xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
          if(!this.xmlHttp)
          {
           this.xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
          }
      }
  }
  if(this.xmlHttp == null)
  {
   alert("invalid xmlHttpRequest!");
   return false;
  }
 }//end function
 
 /**
  * 设置目标请求方法
  */
 this.set_method = function(method)
 {
  if(method != 'POST' && method != 'GET')
  {
   alert("invalid method!");
   return false;
  }
  this.method = method;
 }//end function
 
 /**
  * 获取目标请求方法
  */
 this.get_method = function()
 {
  return this.method;
 }//end function
 
 /**
  * 设置状态该表要触发的事件
  */
 this.set_event = function(event)
 {
  this.event = event;
 }//end function
 
 /**
  * 获取 event
  */
 this.get_event = function()
 {
  return this.event;
 }//end function
 
 /**
  * 设置要请求的目标url
  */
 this.set_url = function(url)
 {
  this.url = url;
 }//end function
 
 /**
  * 获取 url
  */
 this.get_url = function()
 {
  return this.url;
 }//end function
 
 /**
  * 添加REQUEST 参数
  */
 this.add_param = function(key, val)
 {
  var tmpCount = this.params.length;
  this.params[tmpCount] = key + "=" + val;
  }//end function
  
  /**
   * 设置请求为异步连接
   */
  this.set_async = function()
  { 
   this.async = true;
  }//end function
  
  /**
   * 取消异步连接,即设置为同步连接
   */
  this.set_sync = function()
  {
   this.async = false;
  }//end function
  
  /**
  * 发送请求
  */
 this.submit = function()
 {
  this.xmlHttp.onreadystatechange = this.event;
      var send_params = null;
  if(this.params.length > 0)
  {
   send_params = this.params.join('&');
   if(this.method == 'POST')
   {
    this.xmlHttp.open(this.method, this.url, this.async);
    //this.xmlHttp.setRequestHeader("Method", this.method + this.url + " HTTP/1.1");
    this.xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    this.xmlHttp.send(send_params);
   }else
   {
    this.xmlHttp.open(this.method, this.url + "?" + send_params, this.async);
    this.xmlHttp.send(null);
   }
  }else
  {
    this.xmlHttp.open(this.method, this.url, this.async);
    this.xmlHttp.send(send_params);
  }
  
 }//end function
 
 /**
  * return Data
  * @param string type : "xml" or "text"
  */
 this.get_content = function(type)
 {
  if(type == 'xml')
  {
   return this.xmlHttp.responseXML.documentElement;
  }else
  {
   return this.xmlHttp.responseText;
  }
 }
 
 /**
  * 检测事件是否完成
  */
 this.complete = function()
 {
  if(this.xmlHttp.readyState == 4)
  {
   return true;
  }
  return false;
 }//end function
 
 /*
  * 检测请求是否成功
  */
 this.success = function()
 {
  if (this.complete() && this.xmlHttp.status == 200) {
    return true;        
        }
        return false;
 }//end function
 
 /**
  * 获取消息提示
  */
 this.get_statusText = function()
 {
  return this.xmlHttp.statusText;
 }//end function
}//end class

 

ajax类实例

var Ajax = new class_ajax();
  Ajax.ID = "buyTools";
  Ajax.init();
  Ajax.set_method("POST");
  Ajax.set_event(Guess.buyToolsCallBack);   //参数不能加引号
  Ajax.set_url("/iframe/guess_srv.php?action=buy_tools");
  Ajax.add_param("single", single);
  Ajax.add_param("group", group);
  Ajax.add_param("_t", new Date().getTime());
  Ajax.submit();

你可能感兴趣的:(js)