方案一:

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);
        }
    }

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-09-06
  • 2021-12-08
  • 2021-10-11
  • 2022-12-23
  • 2021-12-19
猜你喜欢
  • 2022-12-23
  • 2021-12-02
  • 2021-08-15
  • 2021-04-02
  • 2022-12-23
  • 2021-07-18
相关资源
相似解决方案