Make the console.log() command IE-8 compatible

I often use the command console.log() to trace the execution of my javascript functions in Firebug’s or Safari’s consoles.

The only problem is, Internet Explorer doesn’t understand console.log() and moreover, it doesn’t error out gracefully. It stops the script execution. As a result, it was necessary to be very careful to turn off the console.log() statements whenever a script goes into production.

This can be inconvenient, particularly if you forget, so I was glad to find a way to make Internet Explorer fail more gracefully. The line of code below redefines console.log() to an empty function when it isn’t defined in a given browser.

Even better would be to redefine it to create a little console right on the page, so that it would be easier to trace code in IE as well. I’ll post this when I get it done.

Meanwhile, to make IE understand console.log(), place this line at the top of any scripts that contain console.log() statements:


if (typeof console == "undefined" || typeof console.log == "undefined") var console = { log: function() {} };