小技巧
码检测浏览器是否支持 position:fixed
var div = document.createElement('div');
div.style.cssText = 'display:none;position:fixed;z-index:100;';
body.appendChild(div);
console.log(window.getComputedStyle(div).position != 'fixed');
function isSupportFixed() {
var userAgent = window.navigator.userAgent,
ios = userAgent.match(/(iPad|iPhone|iPod)\s+OS\s([\d_\.]+)/),
ios5below = ios && ios[2] && (parseInt(ios[2].replace(/_/g, '.'), 10) < 5),
operaMini = /Opera Mini/i.test(userAgent),
body = document.body,
div, isFixed;
div = document.createElement('div');
div.style.cssText = 'display:none;position:fixed;z-index:100;';
body.appendChild(div);
isFixed = window.getComputedStyle(div).position != 'fixed';
body.removeChild(div);
div = null;
return !!(isFixed || ios5below || operaMini);
}
高亮所有元素
[].forEach.call($$("*"), function(a) {
a.style.outline =
"1px solid #" + (~~(Math.random() * (1 << 24))).toString(16);
});
计算中文长度
function getRealLen( str ) {
return str.replace(/[^\x00-\xff]/g, '__').length; //这个把所有双字节的都给匹配进去了
}
中文截断
function beautySub( str, len) {
var reg = /[\u4e00-\u9fa5]/g, //专业匹配中文
slice = str.substring(0,len),
realen = len - ( ~~( slice.match(reg) && slice.match(reg).length ) );
return slice.substring(0, realen ? realen : 1);
}
全局错误上报
window.onerror = `function (errorMsg, fileLoc, linenumber) {
var s = 'url: ' + document.URL + '\nfile: ' + fileLoc
+ '\nline number: ' + linenumber
+ '\nmessage: ' + errorMsg;
Log.error(s); // 发给服务器统计监控
console.log(s);
}`