JavaScript技巧大收集(91~100)

不知不觉就收集了100个实用的JavaScript代码片段。。。。

91、原生JavaScript实现窗体改变事件resize的操作(兼容所以的浏览器)

01 (function(){
02     var fn = function(){
03         var w = document.documentElement ? document.documentElement.clientWidth : document.body.clientWidth
04             ,r = 1255
05             ,b = Element.extend(document.body)
06             ,classname = b.className;
07         if(w < r){
08             //当窗体的宽度小于1255的时候执行相应的操作
09         }else{
10             //当窗体的宽度大于1255的时候执行相应的操作
11         }
12     }
13     if(window.addEventListener){
14         window.addEventListener('resize'function(){ fn(); });
15     }else if(window.attachEvent){
16         window.attachEvent('onresize'function(){ fn(); });
17     }
18     fn();
19 })();

92、原生JavaScript用正则清除空格分左右
1 function ltrim(s){ return s.replace( /^(\s*| *)/, ""); }
2 function rtrim(s){ return s.replace( /(\s*| *)$/, ""); }
3 function trim(s){ return ltrim(rtrim(s));}



93、原生JavaScript判断变量是否空值
01 /**
02  * 判断变量是否空值
03  * undefined, null, '', false, 0, [], {} 均返回true,否则返回false
04  */
05 function empty(v){
06     switch (typeof v){
07         case 'undefined' return true;
08         case 'string'    if(trim(v).length == 0) return truebreak;
09         case 'boolean'   if(!v) return truebreak;
10         case 'number'    if(0 === v) return truebreak;
11         case 'object'    :
12             if(null === v) return true;
13             if(undefined !== v.length && v.length==0) return true;
14             for(var in v){return false;} return true;
15             break;
16     }
17     return false;
18 }

94、原生JavaScript实现base64解码
01 function base64_decode(data){
02     var b64 ="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
03     var o1, o2, o3, h1, h2, h3, h4, bits, i = 0,ac = 0,dec = "",tmp_arr = [];
04     if (!data) { return data; }
05     data += '';
06     do {
07         h1 = b64.indexOf(data.charAt(i++));
08         h2 = b64.indexOf(data.charAt(i++));
09         h3 = b64.indexOf(data.charAt(i++));
10         h4 = b64.indexOf(data.charAt(i++));
11         bits = h1 << 18 | h2 << 12 | h3 << 6 | h4;
12         o1 = bits >> 16 & 0xff;
13         o2 = bits >> 8 & 0xff;
14         o3 = bits & 0xff;
15         if (h3 == 64) {
16             tmp_arr[ac++] = String.fromCharCode(o1);
17         else if (h4 == 64) {
18             tmp_arr[ac++] = String.fromCharCode(o1, o2);
19         else {
20             tmp_arr[ac++] = String.fromCharCode(o1, o2, o3);
21         }
22     while (i < data.length);
23     dec = tmp_arr.join('');
24     dec = utf8_decode(dec);
25     return dec;
26 }

95、原生JavaScript实现utf8解码
01 function utf8_decode(str_data){
02     var tmp_arr = [],i = 0,ac = 0,c1 = 0,c2 = 0,c3 = 0;str_data += '';
03     while (i < str_data.length) {
04         c1 = str_data.charCodeAt(i);
05         if (c1 < 128) {
06             tmp_arr[ac++] = String.fromCharCode(c1);
07             i++;
08         else if (c1 > 191 && c1 < 224) {      
09             c2 = str_data.charCodeAt(i + 1);
10             tmp_arr[ac++] = String.fromCharCode(((c1 & 31) << 6) | (c2 & 63));
11             i += 2;
12         else {
13             c2 = str_data.charCodeAt(i + 1);
14             c3 = str_data.charCodeAt(i + 2);
15             tmp_arr[ac++] = String.fromCharCode(((c1 & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
16             i += 3;
17         }
18     }
19     return tmp_arr.join('');
20 }

96、原生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 }

97、原生JavaScript判断IE版本号(既简洁、又向后兼容!)
1 var _IE = (function(){
2     var v = 3, div = document.createElement('div'), all = div.getElementsByTagName('i');
3     while (
4         div.innerHTML = '<!--[if gt IE ' + (++v) + ']><i></i><![endif]-->',
5         all[0]
6     );
7     return v > 4 ? v : false ;
8 }());

98、原生JavaScript获取浏览器版本号
01 function browserVersion(types) {
02     var other = 1;
03     for (i in types) {
04         var v = types[i] ? types[i] : i;
05         if (USERAGENT.indexOf(v) != -1) {
06             var re = new RegExp(v + '(\\/|\\s|:)([\\d\\.]+)''ig');
07             var matches = re.exec(USERAGENT);
08             var ver = matches != null ? matches[2] : 0;
09             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 }

99、原生JavaScript半角转换为全角函数
01 function ToDBC(str){
02   var result = '';
03   for(var i=0; i < str.length; i++){
04     code = str.charCodeAt(i);
05     if(code >= 33 && code <= 126){
06       result += String.fromCharCode(str.charCodeAt(i) + 65248);
07     }else if (code == 32){
08       result += String.fromCharCode(str.charCodeAt(i) + 12288 - 32);
09     }else{
10       result += str.charAt(i);
11     }
12   }
13  return result;
14 }

100、原生JavaScript全角转换为半角函数 
01 function ToCDB(str){
02   var result = '';
03   for(var i=0; i < str.length; i++){
04     code = str.charCodeAt(i);
05     if(code >= 65281 && code <= 65374){
06       result += String.fromCharCode(str.charCodeAt(i) - 65248);
07     }else if (code == 12288){
08       result += String.fromCharCode(str.charCodeAt(i) - 12288 + 32);
09     }else{
10       result += str.charAt(i);
11     }
12   }
13  return result;
14 }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值