天天看点

Js 关于console 在IE 下的兼容问题

程序员在开发代码的过程中,使用console作为调试代码过程的一种手段。

发布到测试生产环境,发现IE8 出现加载错误。使用开发者工具调试,发现可以绕过问题。

通过网络搜索和在项目中进行修正。

以下办法均可生效:

1、去除console代码(由于是调试代码,不影响线上使用)。

2、在基础Js脚本文件中加入:

1 (function() {
 2     var method;
 3     var noop = function () {};
 4     var methods = [
 5         'assert', 'clear', 'count', 'debug', 'dir', 'dirxml', 'error',
 6         'exception', 'group', 'groupCollapsed', 'groupEnd', 'info', 'log',
 7         'markTimeline', 'profile', 'profileEnd', 'table', 'time', 'timeEnd',
 8         'timeline', 'timelineEnd', 'timeStamp', 'trace', 'warn'
 9     ];
10     var length = methods.length;
11     var console = (window.console = window.console || {});
12 
13     while (length--) {
14         method = methods[length];
15 
16         // Only stub undefined methods.
17         if (!console[method]) {
18             console[method] = noop;
19         }
20     }
21 }());      
Js 关于console 在IE 下的兼容问题

喜欢技术、热爱生活。