Debugging Javascript
Posted by ark, ,
So you're used to using alert to debug your javascript. STOP IT! it's
totally lame

Rather than use alert, try a dump instead

dump("Hello World\n");

will print "Hello World" to the console that firefox/mozilla was
launched from (who cares what IE does)

you should set the mozilla config variable (use about:config)
javascript.options.showInConsole to true
and add a new boolean config variable (also set to true)
browser.dom.window.dump.enabled

see:
http://kb.mozillazine.org/Dev_:_Javascript_variables

This really helps in loops and the like when you'd have to close a gazillion alert popups.

-- or --

if you're a greasemonkey user you can use GM_log(message, level) which pops up in the javascript console (along with all your javascript errros) which is really nice

-- or --

just put a little bit of HTML in your page as you debug like this...

<form><textarea style="width: 100%;" rows="25" id="'debug'"></textarea></form>

and add this to your javascript...

/** updates a textarea if there is one - much better than alert */
function debug(msg) {
var ta = document.getElementById('debug');
if (ta) {
ta.value = msg +"\n" + ta.value;
}
}

Comments