在 IE 中测试 console.log 语句 [英] Testing for console.log statements in IE
问题描述
可能的重复:
'console' 是 Internet Explorer 的未定义错误
如果您的代码中有 console.log
语句,Internet Explorer 将抛出 JavaScript 错误(至少在我们的内网用户安装的 IE7 中).
If you have console.log
statements in your code, Internet Explorer will throw a JavaScript error (at least in IE7 which is what our intranet users have installed).
我使用 Firefox 进行大部分开发测试,主要是因为 Firebug 提供的功能(我在其中使用了大量控制台语句),但我也需要在 IE 中进行测试.
I am using Firefox for most of my development testing primarily because of the functionality provided by Firebug (where I use a lot of console statements) but I also need to test in IE.
如果我将以下内容添加到我的 JavaScript 中,则不会抛出错误.
if I add the following to my JavaScript, the error does not get thrown.
var debugging = false;
if (typeof console == "undefined")
var console = { log: function() {} };
问题是如果调试模式为假,我想触发一个事件.如果我创建一个函数来测试调试是否为 false 并执行一个操作(此时只是一个警报),但是当我尝试执行以下操作时,我收到一个 IE 错误,提示 控制台未定义.>
The problem is that I would like to trigger an event if debugging mode is false. If I create a function to test whether debugging is false and do an action (at this point just an alert) but when I try do the following I receive an IE error saying Console is not defined.
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');
}
如果有人可以帮助我修复代码、提供更好的方法来帮助我实现目标,或者将我引导到资源来教育自己,我将不胜感激.
If someone could help me to fix my code, provide a better way to help me achieve my goal, or direct me to a resource to edumacate myself I would be very appreciative.
推荐答案
您不必跳过所有这些障碍.只需在使用前检查控制台是否存在即可.
You don't have to jump through all these hoops. Simply check if the console exists before using it.
所以,而不是:
console.log('foo');
使用:
window.console && console.log('foo');
...你不会得到任何错误.
...and you won't get any errors.
或者,您可以在脚本的顶部检查它,如果它未定义,只需用一个空函数填充它:
Alternatively, you could just check for it at the top of your script, and if it's undefined, just fill it with an empty function:
// 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 的源代码):
For a more robust solution, use this piece of code (taken from twitter's source code):
// 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;
}
}
}());
这篇关于在 IE 中测试 console.log 语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!