Tuesday, June 25, 2013

reminder: console.log() sometimes fails in IE

After hours of debugging a user's issue that I could only occasionally reproduce, I re-discovered that IE only provides a console object once the developer tools have been opened. So, a single debug line that works perfectly well in every other browser on the planet fails without a sound:

console.log("updating checkbox ", checkbox, v)

And it's almost by dumb luck alone that you'd know this line was the issue. Because how would you know? If you've opened the developer tools open, it won't fail. And if they haven't been opened, it fails, but has no console to log the error to ... Monstrously awesome, right?

So, let this be a reminder that IE lame, and it doesn't provide a console object to any of your site's users. So, don't blindly log to the console. Each time you want to log, make sure a console object with a log method exist:

if (window.console && typeof console.log == 'function') console.log("whatever");

Or, create a do-nothing console object once at the top of your page if none exists:

if (!window.console || !console.log) console = {log: function() {}};

/* lots of code ... */

console.log("this won't break your script now ... yay!");

/* lots of other code ... */

5 comments: