首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > JavaScript >

100个直接可以拿来用的JavaScript实用功能代码片断

2013-09-28 
100个直接可以拿来用的JavaScript实用功能代码片段1function cutstr(str, len) { 2var temp 3var icount

100个直接可以拿来用的JavaScript实用功能代码片段

100个直接可以拿来用的JavaScript实用功能代码片断
 1    function cutstr(str, len) { 2         var temp; 3         var icount = 0; 4         var patrn = /[^\x00-\xff]/; 5         var strre = ""; 6         for (var i = 0; i < str.length; i++) { 7             if (icount < len - 1) { 8                 temp = str.substr(i, 1); 9                 if (patrn.exec(temp) == null) {10                     icount = icount + 111                 } else {12                     icount = icount + 213                 }14                 strre += temp15             } else {16                 break17             }18         }19         return strre + "..."20     }
View Code

?

2、原生JavaScript获取域名主机

100个直接可以拿来用的JavaScript实用功能代码片断
 1     function getHost(url) { 2         var host = "null"; 3         if(typeof url == "undefined"|| null == url) { 4             url = window.location.href; 5         } 6         var regex = /^\w+\:\/\/([^\/]*).*/; 7         var match = url.match(regex); 8         if(typeof match != "undefined" && null != match) { 9             host = match[1];10         }11         return host;12 }
View Code

?

3、原生JavaScript清除空格

9 }));3 if (arr != null) return unescape(arr[2]);4 return null5 }];8 }]); 9 return str;10 }]; 8 var versionString = fullVersionString.split('.')[0]; 9 var version = parseInt(versionString);10 11 if(version >= 27){12 return true;13 }14 }15 }16 return false;17 }); 8 if(FixViewPortsExperimentRunning){ 9 return screenWidth / window.innerWidth;10 }else{11 return screenWidth / document.body.offsetWidth;12 }13 } sEvtType,fnHandle); 7 }else{ 8 oTarget["on" + sEvtType] = fnHandle; 9 }10 } sEvtType,fnHandle); 7 }else{ 8 oTarget["on" + sEvtType] = fnHandle; 9 }10 })){ 3 return sStr; 4 } 5 var i,oRs=[],iCode; 6 if(iCase){/*半->全*/ 7 for(i=0; i<sStr.length;i+=1){ 8 iCode = sStr.charCodeAt(i); 9 if(iCode == 32){10 iCode = 12288; 11 }else if(iCode < 127){12 iCode += 65248;13 }14 oRs.push(String.fromCharCode(iCode)); 15 } 16 }else{/*全->半*/17 for(i=0; i<sStr.length;i+=1){ 18 iCode = sStr.charCodeAt(i);19 if(iCode == 12288){20 iCode = 32;21 }else if(iCode > 65280 && iCode < 65375){22 iCode -= 65248; 23 }24 oRs.push(String.fromCharCode(iCode)); 25 } 26 } 27 return oRs.join(""); 28 }this.getMonth()+1, //month 4 "d+" : this.getDate(), //day 5 "h+" : this.getHours(), //hour 6 "m+" : this.getMinutes(), //minute 7 "s+" : this.getSeconds(), //second 8 "q+" : Math.floor((this.getMonth()+3)/3), //quarter 9 "S" : this.getMilliseconds() //millisecond10 }11 if(/(y+)/.test(format)) format=format.replace(RegExp.$1,12 (this.getFullYear()+"").substr(4 - RegExp.$1.length));13 for(var k in o)if(new RegExp("("+ k +")").test(format))14 format = format.replace(RegExp.$1,15 RegExp.$1.length==1 ? o[k] :16 ("00"+ o[k]).substr((""+ o[k]).length));17 return format;18 }19 alert(new Date().format("yyyy-MM-dd hh:mm:ss")); );17 eval("try { win.resizeTo(width, height); } catch(e) { }");18 win.focus();19 }20 }){4 return false;5 }6 }7 return true;8 })6 }catch(e){7 alert(e.message);8 }9 }//若大单位前没有数字则舍去大单位39 str = str.substr(1, str.length - 1);40 str = dw2[k2].concat(str);41 sum = 0;42 }43 }44 if (k1 == 3) //小单位到千则大单位进一45 {46 k2++;47 }48 }49 //转换小数部分50 var strdig = "";51 if (dig != "") {52 var n = dig.charAt(0);53 if (n != 0) {54 strdig += dw[Number(n)] + "角"; //加数字55 }56 var n = dig.charAt(1);57 if (n != 0) {58 strdig += dw[Number(n)] + "分"; //加数字59 }60 }61 str += "元" + strdig;62 } catch(e) {63 return "0元";64 }65 return str;66 }67 //拆分整数与小数68 function splits(tranvalue) {69 var value = new Array('', '');70 temp = tranvalue.split(".");71 for (var i = 0; i < temp.length; i++) {72 value[i] = temp[i];73 }74 return value;75 }100个直接可以拿来用的JavaScript实用功能代码片断
1 function getViewSize(){2     var de=document.documentElement;3     var db=document.body;4     var viewW=de.clientWidth==0 ?  db.clientWidth : de.clientWidth;5     var viewH=de.clientHeight==0 ?  db.clientHeight : de.clientHeight;6     return Array(viewW ,viewH);7 }
View Code

?

97、原生JavaScript判断IE版本号(既简洁、又向后兼容!)

100个直接可以拿来用的JavaScript实用功能代码片断
 1 function browserVersion(types) { 2     var other = 1; 3     for (i in types) { 4         var v = types[i] ? types[i] : i; 5         if (USERAGENT.indexOf(v) != -1) { 6             var re = new RegExp(v + '(\\/|\\s|:)([\\d\\.]+)', 'ig'); 7             var matches = re.exec(USERAGENT); 8             var ver = matches != null ? matches[2] : 0; 9             other = ver !== 0 && v != 'mozilla' ? 0 : other;10         } else {11             var ver = 0;12         }13         eval('BROWSER.' + i + '= ver');14     }15     BROWSER.other = other;16 }
View Code

?

99、原生JavaScript半角转换为全角函数

 1 function ToCDB(str){ 2   var result = ''; 3   for(var i=0; i < str.length; i++){ 4     code = str.charCodeAt(i); 5     if(code >= 65281 && code <= 65374){ 6       result += String.fromCharCode(str.charCodeAt(i) - 65248); 7     }else if (code == 12288){ 8       result += String.fromCharCode(str.charCodeAt(i) - 12288 + 32); 9     }else{10       result += str.charAt(i);11     }12   }13  return result;14 }
View Code

?

热点排行