Javascript Debugging
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.
- JavaScript Console
First thing you should check when there's a problem. To be found under Tools|Web development|JavaScript console. - JavaScript
Debugger (Venkman)
Allows you to stop a script at predefined breakpoints, inspect variables etc.. A walkthrough 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. - strict code checking
If you set the pref "javascript.options.strict" to true, the JavaScript engine gives you more types of warnings on the JavaScript console, most of which hint at code bugs that are easy to oversee or even bad syntax. - dump()
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>; and to have a native console at all under MS Windows, you need to start Mozilla viamozilla.exe -console
. Using normal JS object inspection, you can write a function that dumps a whole object, similar to this ddumpObject(). - Call stack
You can print the current call stack (which functions were called to reach the current point) using the keyworddebugger
. You can also try using Components.stack, which presumably only works in Mozilla from chrome (not webpages).