Is there a way to log to console without breaking code under IE? Is there a way to log to console without breaking code under IE? jquery jquery

Is there a way to log to console without breaking code under IE?


You can create a fake console:

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


IE has its own console, and you wont want to override console if you're using firebug lite. Just make sure that console exists when log gets called:

if (window.console) console.log('foo bar baz', fizz, buzz);

Better yet, use && to shortcut:

window.console && console.log('foo bar baz', fizz, buzz);


I use this snippet myself

if (! ('console' in window) ) {var names = ['log', 'debug', 'info', 'warn', 'error', 'assert', 'dir', 'dirxml', 'group', 'groupEnd', 'time', 'timeEnd', 'count', 'trace', 'profile', 'profileEnd'];window.console = {};for (var i = 0; i < names.length; ++i) window.console[names[i]] = function() {};}else {/*if it exists but doesn't contain all the same methods....silly ie*/var names = ['log', 'debug', 'info', 'warn', 'error', 'assert', 'dir', 'dirxml', 'group', 'groupEnd', 'time', 'timeEnd', 'count', 'trace', 'profile', 'profileEnd'];for (var i = 0; i < names.length; ++i) if(!window.console[names[i]])window.console[names[i]] = function() {};};