Common idiom to avoid IE spill: Error: "console" - undefined

I installed firebug and I wrote all these log statements.

I tested my application in IE and, of course, got a "undefined" error.

What a common idiom to avoid this.

I really don't want to comment on all console.log instructions in my file and not make fun of them.

Well, I'm not sure what to do.

+6
javascript undefined console firebug
source share
2 answers

I usually do this wrapper:

function log(obj) { if (window.console && console.log) console.log(obj); } 

or you could do something like this at the beginning of your file / script element:

 if (!window.console) { window.console = { log: function(obj){ /* define own logging function here, or leave empty */ } }; } 
+10
source share

Paul Irish has the best packaging for console.log() .

http://paulirish.com/2009/log-a-lightweight-wrapper-for-consolelog/

This allows you to use several arguments and provides a history (for debugging) if the console is missing or (for example, Firebug Lite) the console is created later.

+1
source share

All Articles