Sunday, August 26, 2012

Console is not defined

How many times did it happen to you that you can't find bug in your FE code, while client keeps repeating that site looks broken on his PC / Mac. As soon as you test from other box then your own (the one that does not have 'console' object ;)), you see this JS error saying :  'console is not defined'. Whatever it is high-traffic website, or simple web presentation, this WILL break the page. What I like to do, to make sure  even I don't clean up my JavaScript code pages on site / app do not break is to inject a little snippet in every page even if the consumer doesn't have Firebug, Chrome console, or IE developer tools



if(typeof console == 'undefined'){
   var console = {
     log : function(obj){
        return;
     }
   };
}