【问题标题】:Testing for console.log statements in IE [duplicate]在 IE 中测试 console.log 语句 [重复]
【发布时间】:2011-11-26 23:21:38
【问题描述】:

可能重复:
'console' is undefined error for internet explorer

如果您的代码中有console.log 语句,Internet Explorer 将引发 JavaScript 错误(至少在我们的 Intranet 用户已安装的 IE7 中)。

我使用 Firefox 进行大部分开发测试主要是因为 Firebug 提供的功能(我在其中使用了很多控制台语句),但我还需要在 IE 中进行测试。

如果我将以下内容添加到我的 JavaScript,则不会引发错误。

var debugging = false;
if (typeof console == "undefined") 
    var console = { log: function() {} };  

问题是如果调试模式为假,我想触发一个事件。如果我创建一个函数来测试调试是否为假并执行一个操作(此时只是一个警报),但是当我尝试执行以下操作时,我收到一个 IE 错误,提示 Console is not defined。。 p>

var debugging = false; // or true   
if (typeof console == "undefined") 
    var console = { log: function() {consoleMsg()} };   

function consoleMsg() {
    if(!debugging) {
    alert('Console.log event in Production Code');
}   

如果有人可以帮助我修复我的代码,提供更好的方法来帮助我实现目标,或者指导我获得自学的资源,我将非常感激。

【问题讨论】:

标签: javascript internet-explorer firebug


【解决方案1】:

您不必跳过所有这些障碍。只需在使用前检查控制台是否存在。

所以,而不是:

console.log('foo');

用途:

window.console && console.log('foo');

...你不会得到任何错误。


或者,您可以在脚本顶部检查它,如果它未定义,只需用一个空函数填充它:

// At the top of your script:
if ( ! window.console ) console = { log: function(){} };
// If you use other console methods, add them to the object literal above

// Then, anywhere in your script:
console.log('This message will be logged, but will not cause an error in IE7');

要获得更强大的解决方案,请使用这段代码(取自 twitter 的源代码):

// 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]) {
            console[method] = noop;
        }
    }
}());

【讨论】:

  • 如果console 没有定义,那仍然会给出ReferenceError
  • @pimvdb - 忘记添加 window. 部分。谢谢。已更新。
  • 修复了一个小错误:必须是 window.console || (console = { log: function(){} }); 而不是 window.console || console = { log: function(){} };
  • +1 表示取自 Twitter 源代码的一段代码。好地方和分享(并且对来源诚实)。
  • NOT 是否在 IE 上工作 - 您必须调用 typeof(console)。你甚至在 IE6/7/8 上测试过这个吗?
【解决方案2】:

'console' 本身需要是一个函数,'log' 也是如此。所以:

if(typeof(console) === 'undefined') {
    console = function(){};
    console.log = function(){consoleMsg()};
}   

【讨论】:

  • 为什么应该是函数?我试过了:console(),得到了一个错误:object 'console' is not a function
  • 它是Firefox中的一个对象,带有firebug、Chrome、Safari和Opera。
  • 感谢您尝试向此答案添加其他信息,但使用建议的扩展功能创建新答案会更合适。
【解决方案3】:

你试过try-catch:

var debugging = false; // or true 
try {  
    console.log();
} catch(ex) {
    /*var*/ console = { log: function() {consoleMsg()} };   
}

【讨论】:

  • +1 我把它放在我的 js 的顶部,现在我可以在任何地方使用console.log(myVariable),谢谢!
【解决方案4】:
(function(debug) {
    var console;

    function wrapConsoleMethod(fnName) {
        if(fnName in console)
            console[ fnName ] = function(fn) {
                return function() {
                    if(debug)
                        return fn.apply(console, arguments);
                    else
                        alert('Console event in Production Code');
                };
            }(console[ fnName ]);
        else
            ; // fn not in console
    };

    if(!('console' in window))
        window.console = {
            log : function() {}
            // ...
        };
    console = window.console;
    wrapConsoleMethod('log');
    // ...
})(true /* debug */);

console.log('test');

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-24
    • 2019-02-11
    • 1970-01-01
    • 2021-02-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多