防止IE不支持console.log报错

ε祈祈猫儿з 提交于 2019-12-17 20:03:48

方案一:

var console = console || {        log : function() {            return;        }    };//兼容IE,当IE不支持console.log时,自定义一个包含log方法的对象给他

方案二:HTML5 Boilerplate 也提供了一个处理所有的 Console call 的 fallback

// Avoid `console` errors in browsers that lack a console.
(function() {
    var method;
    var noop = function () {};
    var methods = [
        'assert', 'clear', 'count', 'debug', 'dir', 'dirxml', 'error',
        'exception', 'group', 'groupCollapsed', 'groupEnd', 'info', 'log',
        'markTimeline', 'profile', 'profileEnd', 'table', 'time', 'timeEnd',
        'timeStamp', 'trace', 'warn'
    ];
    var length = methods.length;
    var console = (window.console = window.console || {});
    while (length--) {
        method = methods[length];
        // Only stub undefined methods.
        if (!console[method]) {   //对于不支持的方法用noop函数代替
            console[method] = noop;
        }
    }
}());
// Place any jQuery/helper plugins in here.

方案三:自定义封装个方法调用

    function log(msg){
        if (window["console"]){
            console.log(msg);
        }
    }

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!