Possible Duplicate:
'console' is undefined error for internet explorer
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).
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.
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() {} };
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.
'console' itself needs to be a function, as well as 'log'. So:
You don't have to jump through all these hoops. Simply check if the console exists before using it.
So, instead of:
Use:
...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:
For a more robust solution, use this piece of code (taken from twitter's source code):
Did you try
try-catch
: