日期:2014-05-16  浏览次数:20425 次

js公用方法收集(不断更新中)

?

//公用方法 
var VIM={};  
VIM.common = {  
    //转义特殊字符  
    char_cv:function(str){  
        if (str != ''){  
            str = str.replace(/</g,'&lt;');  
            str = str.replace(/%3C/g,'&lt;');  
            str = str.replace(/>/g,'&gt;');  
            str = str.replace(/%3E/g,'&gt;');  
            str = str.replace(/'/g,'&#39;');  
            str = str.replace(/"/g,'&quot;');  
        }  
        return str;  
    },  
    //截取字符串  
    getStr:function(str,len){  
        var length = len || 6;  
        if (str == null) {  
            return 0;  
        }  
        var l = str.length;  
        var blen = 0;  
        var i = 0;  
        for (i = 0; i < l; i++) {  
            if ((str.charCodeAt(i) & 65280) != 0) {  
                blen++;  
                if(blen>length) break;  
            }  
            blen++;  
            if(blen>length) break;  
        }  
        var rStr = str.substring(0,i);  
        if(i<l){  
            rStr += "...";  
        }  
        return rStr;  
    },  
    //页面加载方法,兼容各类浏览器  
    attachEvent:function(elm, evType, callback, useCapture){  
        var obj = typeof elm == "string"?document.getElementById(elm):elm;  
        if (obj.addEventListener) {  
            obj.addEventListener(evType, callback, useCapture);  
            return true;  
        } else if (obj.attachEvent) {  
            var r = obj.attachEvent('on' + evType, callback);  
            return r;  
        } else {  
            obj['on' + evType] = callback;  
        }  
        return false;  
    },  
    getSwfObj:function(id){  
        var r = null;  
        var o = document.getElementById(id);  
        if (o && o.nodeName == "OBJECT") {  
            if (typeof o.SetVariable != UNDEF) {  
                    r = o;  
            }else {  
                var n = o.getElementsByTagName(OBJECT)[0];  
                if (n) {  
                     r = n;  
                }  
            }  
        }  
        return r;  
    },  
    getArgs:function(){  
      var passedArgs = [];  
      var search = self.location.href;  
      search = search.split('?');  
      if(search.length>1){  
        var argList = search[1];  
        argList = argList.split('&');  
        for(var i=0; i<argList.length; i++){  
          var newArg = argList[i];  
          newArg = argList[i].split('=');  
          passedArgs[unescape(newArg[0])] = unescape(newArg[1]);  
        }  
      }  
      return passedArgs;  
    }  
};