views:

857

answers:

9

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?

+2  A: 

Hi ...

I have had many a headache caused by this.

I use console.log() a lot, and until recently, found that it would cause the entire JS code to fail in versions of FF not using firebug.

I usually run a find before going live, and commenting it out.

D

Dashman
+11  A: 

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

Tony Miller
+11  A: 

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.

Gareth Davis
The latest 10.5 versions of Safari actually use the console.log() API in their web dev tools, so this may not be a problem if you're supporting recent versions.
ajm
Fair enough, figured they won't leave it broken for too long
Gareth Davis
+8  A: 

Personally I modified my compressor a while ago to strip out console references pre-compress. A few minutes adding a regex there saves a lifetime of hassle.

annakata
+3  A: 

Just thought I would add a really good tip for any js debugging.... use the keyword "debugger", and its like a breakpoint in the code, firebug detects it also MSIE (if you have visual studio) detects it and as I say its a breakpoint.

Not many people seem to know about this but I have found it invaluble... also if there isnt a debugger installed on the machine that is running the code, nothing happens and the code goes through fine. Although I wouldn't advise leaving them in there.

+2  A: 

Some compressors will strip out any line prefixed by ;;; (which is a legal sequence to have, being three empty statements.) This way you're not strictly limited to console references (i.e. you could do some calculations and then console.log() the result at the end, and the compressor can strip all of them out.) I use JavaScript::Minifier for this.

Kev
A: 

I use this in OOP Javascript, making my own wrapper for log that checks that firebug exists:

myclass.prototype.log = function()
{ 
    if( typeof window.console != 'undefined' )
    {
        console.log.apply( null, arguments ); 
    }
}

Just call:

this.log( arg1, arg2, ...)
voidstate
A: 

Just a reminder that IE Dev Tool does not support apply() on console.log.

Calling console.log.apply() will raise exception in IE8 when dev tool is active.

Du Song
A: 

You can try JavaScript Debug, it is s simple wrapper for console.log http://benalman.com/projects/javascript-debug-console-log/

Maher Saif