It makes sense, but I don’t ever turn Firebug off so I never would have noticed. That is, until I’m sitting in front of a client trying to show off a new website. That’s when I decide to turn it off. Yep.
Turns out console is undefined when Firebug is off (of freaking course) so Firefox stops parsing the code when it gets to all those console.log()‘s and console.assert()‘s that I have so liberally sprinkled my code with. To fix this, wrap ‘em in an if statement, but you shouldn’t use log as a function name since its a built-in. I like clog better anyway.
function clog(string) {
if (window.console) {
console.log(string);
}
}
function assert(fact, explanation) {
if (window.console) {
console.assert(fact, explanation);
}
}
Course this means that text substitution in console.log() doesn’t work, i.e.:
console.log('I like %s', 'pie') // prints ‘I like pie’
but
clog('I like %s', 'pie') // prints 'I like %s'
I’ll think about this and get back to ya.
4 Comments
Doesn’t javascript have some kind of varargs capacity? In Python you could do something like:
def clog( string, *args):
if (window.console):
console.log( string%args)
or rather:
def clog( string, *args):
if True:
print string%args
clog( "This has three args: %s %s %s", 1, 2, 3)
hmmm… looks like the
tag messes with leading whitespace...Yeah, varargs was my first thought too. I’m slowly investigating these two posts:
http://stackoverflow.com/questions/1959040/possible-to-send-javascript-varargs
and
http://www.sitepoint.com/blogs/2008/11/11/arguments-a-javascript-oddity/
While I pretend to work today. Looks like it is possible.