What to do when your javascript only works with Firebug turned on

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.

This entry was posted in Musings. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

4 Comments

  1. Evan Jones
    Posted April 28, 2010 at 1:59 am | Permalink

    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)

  2. Evan Jones
    Posted April 28, 2010 at 2:00 am | Permalink

    or rather:

    def clog( string, *args):
    if True:
    print string%args

    clog( "This has three args: %s %s %s", 1, 2, 3)

  3. Evan Jones
    Posted April 28, 2010 at 2:10 am | Permalink

    hmmm… looks like the tag messes with leading whitespace...

  4. Posted April 30, 2010 at 1:13 pm | Permalink

    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.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>