在IE中测试console.log语句 [英] Testing for console.log statements in IE
问题描述
如果您的代码中包含 console.log
语句,Internet Explorer将抛出JavaScript错误(至少在我们的Intranet用户安装的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,我想触发一个事件。如果我创建一个函数来测试调试是否为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屋!