天天看点

console.logIE8兼容

当你开发控制台时不报错,关闭控制台运行代码时报错。window.console()在ie8中必须打开控制台才能实现,关闭控制台时会报错,在ie9才开始修复该问题。解决这个兼容性问题,有两种方案:

  • 去除页面中的console.log()方法 (我是用这种方法处理的,页面中残留的console.log()不多)
  • 重写window.console()方法;
    (function () {
          var funcs = ['assert', 'clear', 'count', 'debug', 'dir', 'dirxml',
              'error', 'exception', 'group', 'groupCollapsed', 'groupEnd',
              'info', 'log', 'markTimeline', 'profile', 'profileEnd',
              'table', 'time', 'timeEnd', 'timeStamp', 'trace', 'warn'];
          for(var i=0,l=funcs.length;i<l;i++) {
              var func = funcs[i];
              if(!console[func])
                  console[func] = function(){};
          }
      })()
               
js