Firebug: avoid errors in other browsers

Small snippets or full-featured libraries – if you develop in Javascript and don’t have Firefox and Firebug

installed – you’d better get them now.

Firebug is a Firefox extension which brings several top-notch inspection/debugging features. You can inspect DOM, change CSS run-time and so on.

Aside DOM, CSS and Net capabilities Firebug features a very good debugger and some very useful logging capabilities. For example in your scripts you could write:

console.log ( "result is:", var );

and get the javascript variable “var” dumped into Firebug console.

Browsers Issue

Sadly, firebug as an extension is only available to Firefox. This means that any of your script will fail while running inside other browsers.

I wrote a little script to fix the problem:

if ( typeof( console) == "undefined" ) var console = {};
console.turnOff = function ()
{
	var api = [ 'log', 'debug', 'info', 'warn', 'error', 'assert', 'dir', 'dirxml', 'trace', 'group', 'groupEnd', 'time', 'timeEnd', 'profile', 'profileEnd', 'count' ];
	for ( var i = 0; i < api.length; i++ ) console[ api[i] ] = function (){};
	console.firebug = "0.00";
}
if ( typeof( console.firebug) == "undefined"  ) console.turnOff();

Needless to say, I became aware afterwards a quite identical solution was available from the Firebug official site’s as well. Here’s is the official solution:

if (!window.console || !console.firebug)
{
    var names = ["log", "debug", "info", "warn", "error", "assert", "dir", "dirxml",
    "group", "groupEnd", "time", "timeEnd", "count", "trace", "profile", "profileEnd"];

    window.console = {};
    for (var i = 0; i < names.length; ++i)
        window.console[names[i]] = function() {}
}

The only difference is mine provides a console.turnOff() function which disables firebug logging even in firefox (when called explicitly form your script).

console.turnOff();
console.debug( 'Hello world' ); //won't output anything

This seems like a great time to subscribe my RSS !

Leave a Reply

Don’t forget to Subscribe

Bills

Latest Activity

Posts

  • Rapache on Ubuntu 10.04 ? Not likely.
    Along with the last LTS release of Ubuntu, Lucid Lynx, there have been a few requests to bring back Rapache and make it work Read More
  • What is Google ChromeOS, I mean really ?
    All this fuss about Google ChromeOS. Is it a threat to Microsoft ? Is it a threat to the Ubuntu ? (funny nobody wonders if that’s Read More
  • Well said Carla !
    Carla has something to say ’bout the cloud, and I agree 100% with her. Share it !
  • No more Gimp for you little Joe
    When I first posted about the matter, most people’s reaction was LOL. The we thought it was just a random proposal, never going to be Read More
  • Custom iTunes page generator
    Who never dreamed to have his personal Apple website page, like this guy ? Now you can ! UPDATE : they just fixed the page, Read More