文章来源:jquery教程 - http://www.jq-school.com/Show.aspx?id=315
不知不觉就收集了100个实用的JavaScript代码片段,希望可以帮到支持JquerySchool网站的网友和jquery学堂所有群的成员们。
91、原生JavaScript实现窗体改变事件resize的操作(兼容所以的浏览器)
(function(){ var fn = function(){ var w = document.documentElement ? document.documentElement.clientWidth : document.body.clientWidth ,r = 1255 ,b = Element.extend(document.body) ,classname = b.className; if(w < r){ //当窗体的宽度小于1255的时候执行相应的操作 }else{ //当窗体的宽度大于1255的时候执行相应的操作 } } if(window.addEventListener){ window.addEventListener('resize', function(){ fn(); }); }else if(window.attachEvent){ window.attachEvent('onresize', function(){ fn(); }); } fn(); })();
92、原生JavaScript用正则清除空格分左右
function ltrim(s){ return s.replace( /^(\s*| *)/, ""); } function rtrim(s){ return s.replace( /(\s*| *)$/, ""); } function trim(s){ return ltrim(rtrim(s));}
93、原生JavaScript判断变量是否空值
/** * 判断变量是否空值 * undefined, null, '', false, 0, [], {} 均返回true,否则返回false */ function empty(v){ switch (typeof v){ case 'undefined' : return true; case 'string' : if(trim(v).length == 0) return true; break; case 'boolean' : if(!v) return true; break; case 'number' : if(0 === v) return true; break; case 'object' : if(null === v) return true; if(undefined !== v.length && v.length==0) return true; for(var k in v){return false;} return true; break; } return false; }
94、原生JavaScript实现base64解码
function base64_decode(data){ var b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="; var o1, o2, o3, h1, h2, h3, h4, bits, i = 0,ac = 0,dec = "",tmp_arr = []; if (!data) { return data; } data += ''; do { h1 = b64.indexOf(data.charAt(i++)); h2 = b64.indexOf(data.charAt(i++)); h3 = b64.indexOf(data.charAt(i++)); h4 = b64.indexOf(data.charAt(i++)); bits = h1 << 18 | h2 << 12 | h3 << 6 | h4; o1 = bits >> 16 & 0xff; o2 = bits >> 8 & 0xff; o3 = bits & 0xff; if (h3 == 64) { tmp_arr[ac++] = String.fromCharCode(o1); } else if (h4 == 64) { tmp_arr[ac++] = String.fromCharCode(o1, o2); } else { tmp_arr[ac++] = String.fromCharCode(o1, o2, o3); } } while (i < data.length); dec = tmp_arr.join(''); dec = utf8_decode(dec); return dec; }
95、原生JavaScript实现utf8解码
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 = '', 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; }