Escaping Console.log()
- March 20, 2009 3:10 PM
- Javascript
- Comments (2)
Ray Camden just Twittered about checking in code that still has console.log() in the Javascript. He rightly points out that console.log() causes problems for browsers that don't understand it, but you don't really have to have Firebug installed as some browsers like Chrome understand it natively. Rather than try to purge my code of console.log(), and need them again later, my solution is to add an "escape" that lets console.log() evaluate harmlessly on browsers that don't support it. Click more to see the code.
The Javascript is really simple:
if( typeof console == 'undefined' ){
console = {
log: function () {}
};
}
And you can fit it on one line:
if( typeof console == 'undefined' ){ console = { log: function () {} }; }
Technically you should check for the existence of console, and then separately check for the log method, but its working out OK this way.
Comments