兼容和Error

时间:2019-12-20 13:46:27   收藏:0   阅读:81

兼容

IE兼容

if(!Array.prototype.forEach) {
    Array.prototype.forEach = function(fun){
        var len = this.length;
        if(typeof fun != "function"){
            throw new TypeError();
        }
        var thisp = arguments[1];
        for(var i = 0; i < len; i++){
            if (i in this){
                fun.call(thisp, this[i], i, this);  
            } 
        }
    }
}  
String.prototype.trim=function(){
    return this.replace(/(^\s*)|(\s*$)/g,"");
}  
if (!window.JSON) {
    // 添加json对象      
    window.JSON = {
        parse: function(jsonStr) {
            console.log(123)
            return eval('(' + jsonStr + ')');
        },
        stringify: function(jsonObj) {
            var result = '',curVal;
            if (jsonObj === null) {
                return String(jsonObj);
            }
            switch (typeof jsonObj) {
                case 'number':
                case 'boolean':
                    return String(jsonObj);
                case 'string':
                    return '"' + jsonObj + '"';
                case 'undefined':
                case 'function':
                    return undefined;
            }
            switch (Object.prototype.toString.call(jsonObj)) {
                case '[object Array]':
                    result += '[';
                    for (var i = 0, len = jsonObj.length; i < len; i++) {
                        curVal = JSON.stringify(jsonObj[i]);
                        result += (curVal === undefined ? null : curVal) + ",";
                    }
                    if (result !== '[') {
                        result = result.slice(0, -1);
                    }
                    result += ']';
                    return result;
                case '[object Date]':
                    return '"' + (jsonObj.toJSON ? jsonObj.toJSON() : jsonObj.toString()) + '"';
                case '[object RegExp]':
                    return "{}";
                case '[object Object]':
                    result += '{';
                    for (i in jsonObj) {
                        if (jsonObj.hasOwnProperty(i)) {
                            curVal = JSON.stringify(jsonObj[i]);
                            if (curVal !== undefined) {
                                result += '"' + i + '":' +curVal + ',';
                            }
                        }
                    }
                    if (result !== '{') {
                        result = result.slice(0, -1);
                    }
                    result += '}';
                    return result;

                case '[object String]':
                    return '"' + jsonObj.toString() + '"';
                case '[object Number]':
                case '[object Boolean]':
                    return jsonObj.toString();
            }
        }
    };
}  

苹果手机

document.body.addEventListener('click',function (e) {
   if(e.target.type == "text" || e.target.type == "password" || e.target.tagName == "TEXTAREA" ){
    var nowTop = document.documentElement.scrollTop || document.body.scrollTop || 0;
    e.target.addEventListener('blur',mimeBlur)
    function mimeBlur() {
        var e = event || window.event;
        e.target.removeEventListener('blur',mimeBlur)
        setTimeout(function() {
        window.scrollTo(0,nowTop);
        }, 100);
    }
   }
})
<input type="text" unselectable="on" οnfοcus="this.blur();" readonly />
// unselectable属性作用 
// 在IE浏览器中,当input获得焦点时,点击有unselectable=”on”属性的标签时,不会触发onblur事件。 
// onfocus=”this.blur()”方法作用 
// 获取焦点时调用失去焦点事件

安卓手机

微信浏览器

//这也不行就得用接口签名之后再ready里执行play了
document.addEventListener("WeixinJSBridgeReady", function () {
     audio.play();
}, false);
// location.reload()无效
location.href = location.href  + '_t=' + new Date().getTime()

上面的是开发过程中遇到的,其他文章有
浏览器兼容性问题解决方案
移动开发兼容问题整理笔记
移动开发软键盘兼容

错误捕获

文章来自
错误拦截插件,付费

常见错误

错误捕获方式

/**
* @param {String}  message    错误信息
* @param {String}  source    出错文件
* @param {Number}  lineno    行号
* @param {Number}  colno    列号
* @param {Object}  error  Error对象(对象)
*/
window.onerror = function(message, source, lineno, colno, error) {
   console.log('捕获到异常:',{message, source, lineno, colno, error});
}
window.addEventListener('error', (error) => {
    console.log('捕获到异常:', error);
}, true)
window.addEventListener("unhandledrejection", function(e){
  e.preventDefault()
  console.log('捕获到异常:', e);
  return true;
});
Vue.config.errorHandler = (err, vm, info) => {
  console.error('通过vue errorHandler捕获的错误');
  console.error(err);
  console.error(vm);
  console.error(info);
}

原文:https://www.cnblogs.com/pengdt/p/12072487.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!