Julian Lannigan

console.log() – A lightweight Firebug error protection function { 0 }

I did some searching for a function to keep IE from barking at the user if they didn’t have Firebug or Console API in their browser.  And their are a ton of simplistic functions that turned up that usually just cover the log, warn, error, and info functions of the console API.  I like to use the trace, assert and grouping functions with my JavaScript, just because I am a code nazi when it comes to debugging a site full of AJAX.  Let me tell you grouping debug messages is a life saver sometimes!

I needed a quick small function that declares all the available functions in the Console API, when the API is not available in the browser. I did not necessarily want a “wrapper” function for the current project I was working on. Here is the code used to protect non-Console API browsers from freaking out when they come across a

console.log('message');

The browser will just run an empty function.

(function(){
    var a = function(){};
    try
        {console.assert(1)}
    catch(e){
        window.console={
            log:a,assert:a,debug:a,info:a,warn:a,
            error:a,dir:a,dirxml:a,trace:a,
            group:a,groupCollapse:a,groupEnd:a,
            time:a,timeEnd:a,profile:a,profileEnd:a,
            count:a,exception:a,table:a
        }
    }
}())

Compressed version:
http://pastie.org/1152667

*8 is bit of an improvement, but it’s still Internet Exploder (I’m sorry Explorer).

Be Sociable, Share!

Leave a Reply