Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript best practice: handling Firebug-specific code

Firebug is certainly a wonderful tool for javascript debugging; I use console.log() extensively.

I wanted to know if I can leave the Firebug-specific code in production. What's the best practice? Commenting the debug code?

like image 972
Dan Burzo Avatar asked May 27 '09 12:05

Dan Burzo


2 Answers

If you leave console.log() calls in your production code, then people visiting the site using Internet Explorer will have JavaScript errors. If those people have extra debugging tools configured, then they will see nasty dialog boxes or popups.

A quick search revealed this thread discussing methods to detect if the Firebug console exists: http://www.nabble.com/Re:-detect-firebug-existance-td19610337.html

like image 121
Tony Miller Avatar answered Nov 15 '22 13:11

Tony Miller


been bitten by this before. Ideally all the console.log statements need to be removed before production, but this is error prone and developers invariably forget or only test in FF + Firebug.

A possible solution is to create a dummy console object if one isn't already defined.

if( typeof window.console == 'undefined'){
    window.console = {
        log:function(){}
    };
}

One word of caution: It used to be the case for Safari on 10.4 that any call to console.log would throw a security exception as the console object is a reserved object used in the Mac OS Dashboard widgets. Not sure this is the case anymore, will check tonight.

like image 36
Gareth Davis Avatar answered Nov 15 '22 15:11

Gareth Davis