sprugman
sprugman

Reputation: 19831

best way in javascript to make console.log calls not cause problems when there's no console?

I'm using firebug and making lots of console.log, .info, .dir calls, etc. When the app runs on a machine with firebug turned off, it can cause errors. What's the best technique to avoid that? This seems to work:

// global scope
if (typeof(console) == 'undefined') {
    console = {
        info : function() {},
        dir : function() {},
        error : function() {},
        log : function() {}
    };
}

but I don't like the idea of manually maintaining a list of console functions. Other ideas?

(We've also got jQuery on the project if that helps.)

Upvotes: 4

Views: 1146

Answers (1)

Nick Craver
Nick Craver

Reputation: 630569

I personally just use $.noop to shorten it like this:

if(!window.console)
  window.console = { log: $.noop, group: $.noop, groupEnd: $.noop };

But whatever functions you're using, add them in.

Upvotes: 3

Related Questions