100个不能错过的实用JS自定义函数(12)


function utf8_decode(str_data){
  var tmp_arr = [],i = 0,ac = 0,c1 = 0,c2 = 0,c3 = 0;str_data += '';
  while (i < str_data.length) {
    c1 = str_data.charCodeAt(i);
    if (c1 < 128) {
      tmp_arr[ac++] = String.fromCharCode(c1);
      i++;
    } else if (c1 > 191 && c1 < 224) {
      c2 = str_data.charCodeAt(i + 1);
      tmp_arr[ac++] = String.fromCharCode(((c1 & 31) << 6) | (c2 & 63));
      i += 2;
    } else {
      c2 = str_data.charCodeAt(i + 1);
      c3 = str_data.charCodeAt(i + 2);
      tmp_arr[ac++] = String.fromCharCode(((c1 & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
      i += 3;
    }
  }
  return tmp_arr.join('');
}


96、原生JavaScript获取窗体可见范围的宽与高

复制代码 代码如下:


function getViewSize(){
  var de=document.documentElement;
  var db=document.body;
  var viewW=de.clientWidth==0 ?  db.clientWidth : de.clientWidth;
  var viewH=de.clientHeight==0 ?  db.clientHeight : de.clientHeight;
  return Array(viewW ,viewH);
}


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

复制代码 代码如下:


var _IE = (function(){
    var v = 3, div = document.createElement('div'), all = div.getElementsByTagName('i');
    while (
        div.innerHTML = '<!--[if gt IE ' + (++v) + ']><i></i><![endif]-->',
        all[0]
    );
    return v > 4 ? v : false ;
}());


98、原生JavaScript获取浏览器版本号

复制代码 代码如下:


function browserVersion(types) {
    var other = 1;
    for (i in types) {
        var v = types[i] ? types[i] : i;
        if (USERAGENT.indexOf(v) != -1) {
            var re = new RegExp(v + '(\\/|\\s|:)([\\d\\.]+)', 'ig');
            var matches = re.exec(USERAGENT);
            var ver = matches != null ? matches[2] : 0;
            other = ver !== 0 && v != 'mozilla' ? 0 : other;
        } else {
            var ver = 0;
        }
        eval('BROWSER.' + i + '= ver');
    }
    BROWSER.other = other;
}


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

复制代码 代码如下:


function ToDBC(str){
  var result = '';
  for(var i=0; i < str.length; i++){
    code = str.charCodeAt(i);
    if(code >= 33 && code <= 126){
      result += String.fromCharCode(str.charCodeAt(i) + 65248);
    }else if (code == 32){
      result += String.fromCharCode(str.charCodeAt(i) + 12288 - 32);
    }else{
      result += str.charAt(i);
    }
  }
 return result;
}


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

复制代码 代码如下:


function ToCDB(str){
  var result = '';
  for(var i=0; i < str.length; i++){
    code = str.charCodeAt(i);
    if(code >= 65281 && code <= 65374){
      result += String.fromCharCode(str.charCodeAt(i) - 65248);
    }else if (code == 12288){
      result += String.fromCharCode(str.charCodeAt(i) - 12288 + 32);
    }else{
      result += str.charAt(i);
    }
  }
 return result;
}

您可能感兴趣的文章:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wdydzj.html