Javascript best practice: handling Firebug-specific code

☆樱花仙子☆ 提交于 2019-12-02 20:58:03
Tony Miller

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

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.

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.

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.

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

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.

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, ...)

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.

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!