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[i]; 
   } 
   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[i].indexOf('=');          // Look for "name=value"
        if (pos == -1) continue;                  // If not found, skip
        var argname = pairs[i].substring(0,pos);  // Extract the name
        var value = pairs[i].substring(pos+1);    // Extract the value
        value = decodeURIComponent(value);        // Decode it, if needed
        args[argname] = 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[1];          
};

 

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);
    }    
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值