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 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, or set the "Enable chrome and add-on debugging" option in the developer tool settings. Activate it through with the menu Tools > Web Developer > Browser Console.

You can also start the Browser Console when you launch Firefox, by launching Firefox from the command line and passing --jsconsole as a flag:

/path/to/firefox --jsconsole

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

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

Error Console

This is obsolete and is no longer enabled in Firefox by default. Use the Web Console or the Browser Console instead.

Browser 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:configand set the following two prefs:

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

After you restart the browser, you can access the Browser Debugger through Tools > Web Developer > Browser Toolbox. (Note that before Firefox 28, this was labeled "Browser Debugger" and only the debugger was available, not the whole toolbox.)

Access the "Browser Toolbox" through the menu

Note that you must accept the incoming connection :

Accept the incoming connection from the toolbox

(You may disable the pop-up above with the devtools.debugger.prompt-connection set to false in about:config. This can be a security risk, especially if you also set the devtools.debugger.force-local preference to false.)

Then, the Browser Toolbox displays the available tools for debugging. (Note that before Firefox 28, only the script debugger was available.) The use of these tools are the same as the Built-in Tools.


The connected browser toolbox

Console.log in Browser Console

You can dump variables in the Browser Console from addon code, by adding this line to import the console utility:

const { console } = Components.utils.import("resource://gre/modules/devtools/Console.jsm", {});

This has an advantage over dump in that you can list out properties of an object logged with console.log.

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

Log.jsm (formerly log4moz)

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

Components.utils.import("resource://gre/modules/Log.jsm");

"debugger" keyword

You can halt Venkman or Chromebug at a line 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");
}

Original Document Information

See Also

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:54 次

字数:8252

最后编辑:6 年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文