Firebug “console is undefined”
If you’re using Firebug and don’t want to bother removing or commenting out all the console.log debug messages for users who aren’t, put this at the top:
if(typeof(console) === "undefined" || typeof(console.log) === "undefined") var console = { log: function() { } };
This reminds me of the trick I use for C/C++ of putting debugging statements inside a macro:
D(cerr<<"The number is "<<n<<endl;);
where the macro D is defined as
#define D(A) A
when you want the debugging code to run, and
#define D(A)
when you don’t. :-)
(The final semicolon after the D() is for Emacs to indent it reasonably.)
Update: Of course, the above are just hacky hacks to save typing. The “right” way for conditional C code is usually to use #ifdef DEBUG and the like, and the right way around the Firebug problem is to use something more general like this code at metamalcolm.
[…] programming. Tagged: FireBug, javascript, programming. Leave a Comment This is a response to Shreevatsa’s post of the same […]
FireBug “console is undefined” « Malcolm on Malcolm
Thu, 2009-08-06 at 14:10:02
For some reason I found this didn’t seem to want to work if you add all the parts of Firebug console into it, so ended up with:
if (typeof(console) === “undefined” ||
typeof(console.log) === “undefined” ||
typeof(console.error) === “undefined”) {
var console = new Object();
console.log = function(fakeinput) { };
console.error = function(fakeinput) { };
console.info = function(fakeinput) { };
console.debug = function(fakeinput) { };
console.warn = function(fakeinput) { };
}
…which does seem to work.
Original only affected however if calling more than one of the console types, eg:
console.error(‘xxxxxx’);
console.info(‘xxxxxx’);
…whcih will then tell me that console.error() is not defined. Not worked out why. Blooming J/Scripts. :)
Trev
Trev
Fri, 2009-11-27 at 11:53:53