huanyq2008 发表于 2013-1-29 11:47:05

JS函数库 common.js

/************************************************************ 相关函数说明 $(id) 根据元素id返回对象 GetLen(str) 计算字符串的长度,汉字占两个字符 InitHeight(obj) 自适应Iframe高度,Iframe元素的高度随里面的内容自适应高度 Close() 关闭窗口,FF默认不支持窗口关闭 GetElementByName(name, i) 根据元素名称返回对象,可能会有多个元素同名,i表示序号 ArrayRemove(array, n) 删除数组中指定位置的元素 ************************************************************/ function $(id) {    return document.getElementById(id); } function GetLen(str) {    return str.replace(/[^\x00-\xff]/g, '**').length; } function InitHeight(obj) {    obj.style.height = (obj.contentWindow.document.body.scrollHeight + 20) + 'px'; } function Close() {    window.opener = null;    window.open('', '_self');    window.close(); } // 根据元素名称返回对象 function GetElementByName(name, i) {    if (typeof(i) == 'undefined') i = 0;    var els = document.getElementsByName(name);    if (els.length > 0)    {      return els;    }    return null; } function ArrayRemove(array, n) {    if(n < 0 || n >= array.length) return array;    // slice 方法返回数组的一段    // concat 方法将当前数组和指定的一个或多个数组整成一个    return array.slice(0, n).concat(array.slice(n + 1, array.length)); } /************************************************************ Cookie 静态类。 功能:操作 Cookie。 1.Cookie.Exist(name) 检查 Cookie 是否存在 2.Cookie.Get(name) 读取 Cookie 的值,没有该 Cookie 返回 null 3.Cookie.Remove(name, path, domain) 删除 Cookie 4.Cookie.Add(name, value, expires, path, domain, secure) 添加 Cookie,更新和添加都一样 5.Cookie.Append(name, value) 追加值,用"|"分隔 ************************************************************/ var Cookie = {    Expires : 365,    Exist : function(name)    {      if (this.Get(name)) return true;      return false;    },    Get : function(name)    {      var strCookies = document.cookie;      var cookieName = name + '=';// Cookie名称                        // 取得值的开始位置      var valueBegin = strCookies.indexOf(cookieName);      if (valueBegin == -1) return null;// 没有此Cookie                // 取得值的结尾位置      var valueEnd = strCookies.indexOf(';', valueBegin);      if (valueEnd == -1) valueEnd = strCookies.length; // 是最后一个Cookie      // 取得Cookie值      var value = strCookies.substring(valueBegin + cookieName.length, valueEnd);      return value;    },    Add : function(name, value, expires)    {      if (typeof(expires) == 'undefined') expires = this.Expires;      var strCookie = name + '=' + value;      if (expires)      {            // 计算Cookie的期限, 参数为天数            var curTime = new Date();            curTime.setTime(curTime.getTime() + expires*24*60*60*1000);            strCookie += '; expires=' + curTime.toGMTString();      }      document.cookie = strCookie;    },    Append : function(name, value, expires) // 值用“|”分隔    {      var oldValue = this.Get(name);      if (oldValue != null) value = oldValue + '|' + value;      this.Add(name, value, expires);    },    Remove : function(name)    {      var strCookie;      // 检查Cookie是否存在      if (this.Exist(name))      {            // 设置Cookie的期限为己过期            strCookie = name + '=;';            document.cookie = strCookie;      }    } } /************************************************************ Ajax 静态类。 功能:操作 XMLHttpRequest,实现 Ajax Ajax.LoadXml(url, method, callback) url 请求的地址 method 请求的方法,如GET或POST callback 请求响应后,回调的方法,这个是自定义的函数名称 ************************************************************/ var Ajax = {    LoadXml : function(url, method, callback)    {      var xmlHttp;      if (window.ActiveXObject) // IE      {            xmlHttp = new ActiveXObject('Microsoft.XMLHTTP');      }      else if (window.XMLHttpRequest) // FF      {            xmlHttp = new XMLHttpRequest();      }      if(!xmlHttp)      {            window.alert('不能创建 XMLHttpRequest 对象!');            return false;      }      xmlHttp.open(method, url, true);      xmlHttp.onreadystatechange = function()      {            if(xmlHttp.readyState != 4) return;            callback(xmlHttp);      }      xmlHttp.send(null);    } }  
 
2010.11.9添加:
//获取url参数function getArgs( ) {    var args = new Object( );    var query = location.search.substring(1);   // Get query string    var pairs = query.split("&");               // Break at ampersand    for(var i = 0; i < pairs.length; i++) {      var pos = pairs.indexOf('=');          // Look for "name=value"      if (pos == -1) continue;                  // If not found, skip      var argname = pairs.substring(0,pos);// Extract the name      var value = pairs.substring(pos+1);    // Extract the value      value = decodeURIComponent(value);      // Decode it, if needed      args = value;                  // Store as a property    }    return args;                                  // Return the object}function GetQueryString( sProp ) {var re = new RegExp( sProp + "=([^\\&]*)", "i" );var a = re.exec( document.location.search );if ( a == null )return "";return a;          }; 
2010-12-11
function clickForAuto(){    var nodeId = "test";    if(document.all){    document.getElementById(nodeId).click();    }else{    var evt = document.createEvent("MouseEvents");    evt.initEvent("click", true, true);    document.getElementById(nodeId).dispatchEvent(evt);    }    } 
页: [1]
查看完整版本: JS函数库 common.js