February 16, 2008

Console dot what?

By now we have all began to use console.log. Today, I want to remind you that there is more to Firebug's console function. Here is a list from the documentation. Joe Hewitt has provided us with a treasure chest of web development goodies; but not everyone has firebug. I like to test my scripts on other browsers in parallel with Firefox, and sometimes my console statements stay in the code. Calling console.log will give a fatal error if console is not found, so put this snippet somewhere in your base javascript file:

/*****************************
*
* window.console fix
*
****************************/
window.console = (typeof console == 'undefined') ? {
log: function(t) { alert(t); },
info: function() { },
debug: function() { }
} : console;
From what I have seen, it is the most reliable way to accomplish this. If you use such methods as console.count, simply add that function into my snippet. I got this idea from dustin diaz in this post in his comment to Felix. He said to use:

window.console = console || {
log: function() { },
info: function() { },
debug: function() { }
};

However, this does not work, as it tries to access console before his custom object, giving fatal errors in browsers such as IE 6.

I encourage you to find new ways to let firebug help you, because to become great, you must build on the work of others.

No comments: