Debugging JavaScript

  • Revision slug: Debugging_JavaScript
  • Revision title: Debugging JavaScript
  • Revision id: 465173
  • Created:
  • Creator: PikadudeNo1
  • Is current revision? No
  • Comment

Revision Content

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 when you're debugging a web page; open the Web console using the Web Console option in the Web Developer menu. This shows any JavaScript errors in your app, as well as any logging calls from the console API.

Browser Console

The Browser Console lets you see all JavaScript errors and logging in the browser, including from Firefox code. To enable it, go to about:config in the url bar and set devtools.chrome.enabled to true. Activate it through with the menu Web Developer > Browser Console.

Log to the Browser Console using the standard console API after importing Console.jsm:

let console = (Cu.import("resource://gre/modules/devtools/Console.jsm", {})).console;
console.log("Hello from Firefox code");

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 (Built-in)

On Firefox 19 or later, it's possible to use the built-in JS debugger on the browser itself.  Go to about:config and set the following two prefs:

devtools.chrome.enabled: true
devtools.debugger.remote-enabled: true

After you restart the browser, the Web Developer menu will contain a "Browser Debugger" entry.

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).

"debugger" keyword

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. Change the scratchpad to evaluate in the context of the current browser window by setting Environment > Browser in the menu.

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.

 

Revision Source

<p>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.</p>
<h3 id="Web_Console"><a href="/en/Using_the_Web_Console" title="en/Using the Web Console">Web Console</a></h3>
<p>This is the first place to go when you're debugging a web page; open the Web console using the <strong>Web Console</strong> option in the <strong>Web Developer</strong> menu. This shows any JavaScript errors in your app, as well as any logging calls from the <a href="/en-US/docs/Web/API/console" title="/en-US/docs/Web/API/console">console API.</a></p>
<h3 id="Browser_Console"><a href="/en-US/docs/Tools/Browser_Console" title="/en-US/docs/Tools/Browser_Console">Browser Console</a></h3>
<p>The Browser Console lets you see all JavaScript errors and logging in the browser, including from Firefox code. To enable it, go to <code>about:config</code> in the url bar and set <code>devtools.chrome.enabled</code> to<code> true</code>. Activate it through with the menu <strong>Web Developer</strong> &gt; <strong>Browser Console</strong>.</p>
<p>Log to the Browser Console using the standard <code>console </code>API after importing Console.jsm:</p>
<pre class="brush: js">
let console = (Cu.import("resource://gre/modules/devtools/Console.jsm", {})).console;
<code>console.log("Hello from Firefox code");</code></pre>
<h3 id="Error_Console" name="Error_Console"><a href="/en/Error_Console" title="en/Error_Console">Error Console</a></h3>
<p>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 <span style="font-family: Courier New;">-jsconsole</span> on the command line.</p>
<h3 id="JavaScript_Debugger" name="JavaScript_Debugger"><a href="/en-US/docs/Tools/Debugger" title="Tools/Debugger">JavaScript Debugger (Built-in)</a></h3>
<p>On Firefox 19 or later, it's possible to use the built-in JS debugger on the browser itself.&nbsp; Go to <code>about:config </code>and set the following two prefs:</p>
<pre>
<code>devtools.chrome.enabled: true</code>
<code>devtools.debugger.remote-enabled: true</code></pre>
<p>After you restart the browser, the Web Developer menu will contain a "Browser Debugger" entry.</p>
<h3 id="Strict_code_checking" name="Strict_code_checking">Strict code checking</h3>
<p>If you set the pref <code>javascript.options.strict</code> 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).</p>
<h3 id="dump.28.29" name="dump.28.29"><code>dump()</code></h3>
<p>The <code>dump()</code> function allows you to print text on the native console. Use <code>\n</code> to output a newline at the end.</p>
<p>To see anything, you need to set the pref <code>browser.dom.window.dump.enabled</code> to true, e.g. in <code>about:config</code> (add new pref, it doesn't exist per default).</p>
<p>Under Microsoft Windows you additionally need to start Firefox via the following command to have a native console :</p>
<pre>
firefox.exe -console
</pre>
<p>Using normal JavaScript object inspection, you can write a function that dumps a whole object, similar to this <code><a class="external" href="http://mxr.mozilla.org/mozilla/source/extensions/sroaming/resources/content/transfer/utility.js#426" title="http://mxr.mozilla.org/mozilla/source/extensions/sroaming/resources/content/transfer/utility.js#426">ddumpObject()</a></code>.</p>
<h3 id="log4moz"><a class="link-https" href="https://wiki.mozilla.org/Labs/JS_Modules#Logging" title="https://wiki.mozilla.org/Labs/JS_Modules#Logging">log4moz</a></h3>
<p>This is a partial implementation of the Log4* interfaces (for example, see <a class="external text" href="http://logging.apache.org/log4j/1.2/index.html" rel="nofollow">log4j</a> or <a class="external text" href="http://logging.apache.org/log4net/" rel="nofollow">log4net</a>).</p>
<h3 id="Call_stack" name="Call_stack">"debugger" keyword</h3>
<p>You can halt Venkman or Chromebug at a line using using the keyword <a class="external" href="http://www.mozilla.org/scriptable/javascript-stack-dumper.html"><code>debugger</code></a>. 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 <code><a href="/en/Components.stack" title="en/Components.stack">Components.stack</a></code> like this:</p>
<pre class="brush: js">
function getStackDump() {
  var lines = [];
  for (var frame = Components.stack; frame; frame = frame.caller) {
    lines.push(frame.filename + " (" + frame.lineNumber + ")");
  }
  return lines.join("\n");
}
</pre>
<h3 id="Scratchpad">Scratchpad</h3>
<p>Scratchpad has chrome privileges if <code>devtools.chrome.enabled = true</code>. Change the scratchpad to evaluate in the context of the current browser window by setting <strong>Environment</strong> &gt; <strong>Browser</strong> in the menu.</p>
<h3 id="See_Also">See Also</h3>
<ul>
  <li><a href="/en/Debugging_Mozilla_with_gdb" title="en/Debugging Mozilla with gdb">Debugging Mozilla with gdb</a></li>
  <li><a href="/en/Setting_up_extension_development_environment" title="en/Setting up extension development environment">Setting up an extension development environment</a> (particularly <a href="/en/Setting_up_extension_development_environment#Development_preferences" title="en/Setting up extension development environment#Development preferences">development preferences</a> and <a href="/en/Setting_up_extension_development_environment#Development_extensions" title="en/Setting up extension development environment#Development extensions">development extensions</a>)</li>
</ul>
<div class="originaldocinfo">
  <h2 id="Original_Document_Information" name="Original_Document_Information">Original Document Information</h2>
  <ul>
    <li>Author(s): Ben Bucksch</li>
    <li>Created Date:&nbsp;September 12, 2005, Last Updated Date: November 10, 2009</li>
    <li>Copyright Information: Portions of this content are © 1998–2007 by individual mozilla.org contributors; content available under a Creative Commons license | <a class="external" href="http://www.mozilla.org/foundation/licensing/website-content.html">Details</a>.</li>
  </ul>
</div>
<div id="livemargins_control" style="position: absolute; display: none; z-index: 9999;">
  <img alt="" height="5" src="chrome://livemargins/skin/monitor-background-horizontal.png" style="position: absolute; left: -77px; top: -5px;" width="77" /> <img alt="" src="chrome://livemargins/skin/monitor-background-vertical.png" style="position: absolute; left: 0pt; top: -5px;" /> <img alt="" id="monitor-play-button" src="chrome://livemargins/skin/monitor-play-button.png" style="position: absolute; left: 1px; top: 0pt; opacity: 0.5; cursor: pointer;" /></div>
<p>&nbsp;</p>
<div id="livemargins_control" style="position: absolute; display: none; z-index: 9999;">
  <img alt="" height="5" src="chrome://livemargins/skin/monitor-background-horizontal.png" style="position: absolute; left: -77px; top: -5px;" width="77" /> <img alt="" src="chrome://livemargins/skin/monitor-background-vertical.png" style="position: absolute; left: 0pt; top: -5px;" /> <img alt="" id="monitor-play-button" src="chrome://livemargins/skin/monitor-play-button.png" style="position: absolute; left: 1px; top: 0pt; opacity: 0.5; cursor: pointer;" /></div>
<div id="livemargins_control" style="position: absolute; display: none; z-index: 9999;">
  <img alt="" height="5" src="chrome://livemargins/skin/monitor-background-horizontal.png" style="position: absolute; left: -77px; top: -5px;" width="77" /> <img alt="" src="chrome://livemargins/skin/monitor-background-vertical.png" style="position: absolute; left: 0pt; top: -5px;" /> <img alt="" id="monitor-play-button" src="chrome://livemargins/skin/monitor-play-button.png" style="position: absolute; left: 1px; top: 0pt; opacity: 0.5; cursor: pointer;" /></div>
Revert to this revision