Debugging JavaScript

This document is intended to help developers writing JavaScript code in Mozilla, mainly for Mozilla itself, but it may also be useful for web developers. It should give pointers to tools, aids and tricks which make debugging your code easier.

Web Console

This is the first place to go; open the Web console using the Web Console option in the Tools menu. This opens the console in the top portion of your browser window. To get chrome privileges, open the Web Console from a privileged page; about:about works well for this. You can even make the global window object a ChromeWindow with a bit of hacking.

Error Console

On versions of Firefox predating the Web Console, this is the first thing you should check when there's a problem. You can get to this by choosing the Error Console menu item under the Tools menu or using -jsconsole on the command line.

JavaScript Debugger (Venkman)

Allows you to stop a script at predefined breakpoints, inspect variables etc.. A Venkman Introduction explains its use.

DOM Inspector

Shows the current state of the DOM (page/dialog structure as objects). Other modes of the right pane show the styles applied to the elements as well as the associated JavaScript objects. The button on the top left allows you to pick an element in the live page. With Ctrl-Shift-I, you can inspect the page currently loaded in the browser.

Chromebug

An adaptation of Firebug to XUL, Chromebug integrates DOM inspection and JavaScript debugging. See the Chromebug User Guide.

Strict code checking

If you set the pref javascript.options.strict to true, the JavaScript engine gives you more types of warnings on the Error Console, most of which hint at code bugs that are easy to oversee or even bad syntax. (The pref also warns on common JavaScript idioms that are not errors).

dump()

The dump() function allows you to print text on the native console. Use \n to output a newline at the end.

To see anything, you need to set the pref browser.dom.window.dump.enabled to true, e.g. in about:config (add new pref, it doesn't exist per default).

Under Microsoft Windows you additionally need to start Firefox via the following command to have a native console :

firefox.exe -console

Using normal JavaScript object inspection, you can write a function that dumps a whole object, similar to this ddumpObject().

log4moz

This is a partial implementation of the Log4* interfaces (for example, see log4j or log4net).

Call stack

You can halt Venkman or Chromebug at a line using using the keyword debugger. In debug builds this also dumps a stack trace to the console, even when the debugger is not running. In extensions you can print the callstack using Components.stack like this:

function getStackDump() {
  var lines = [];
  for (var frame = Components.stack; frame; frame = frame.caller)
    lines.push(frame.filename + " (" + frame.lineNumber + ")");return lines.join("\n");
};

Scratchpad

Scratchpad has chrome privileges if devtools.chrome.enabled = true.

See Also

Original Document Information

  • Author(s): Ben Bucksch
  • Created Date: September 12, 2005, Last Updated Date: November 10, 2009
  • Copyright Information: Portions of this content are © 1998–2007 by individual mozilla.org contributors; content available under a Creative Commons license | Details.

 

Tags (3)

Edit tags

Attachments (0)

 

Attach file