Error Console: Difference between revisions

From MozillaZine Knowledge Base
Jump to navigationJump to search
m (tweak the code; add a link to JS coding guidelines)
No edit summary
Line 1: Line 1:
You can output information to console using the following method:
{{extdev}}
 
You can print information to JavaScript Console using the following method:


<ol>
<ol>
<li>Declare a global variable (but be sure to use an [[Dev : Javascript coding guidelines|unique identifier]]!):
<li>Declare a variable (global if you're scripting your own window or [[Dev : Javascript coding guidelines|your object's member]] if you're overlaying an existing window):
<pre>var gConsoleService = Components.classes['@mozilla.org/consoleservice;1']
<pre>var gConsoleService = Components.classes['@mozilla.org/consoleservice;1']
                     .getService(Components.interfaces.nsIConsoleService);</pre>
                     .getService(Components.interfaces.nsIConsoleService);</pre>
(note, you can also use <tt>toJavaScriptConsole()</tt> function to open JavaScript console on startup)
(note, that you can use <tt>toJavaScriptConsole()</tt> function to open JavaScript console on startup)
</li>
</li>


Line 19: Line 21:
</ol>
</ol>


See also: [[Dev : Tips : Enabling dump|Using dump()]], [http://www.mozilla.org/projects/xpcom/using-consoleservice.html Using Console Service].
See also: [[Viewing dump() output]], [http://www.mozilla.org/projects/xpcom/using-consoleservice.html Using Console service].
 
[[Category:Development]]

Revision as of 02:17, 19 December 2004

This page is part of the extension development documentation project.

Ask your questions in MozillaZine Forums. Also try browsing example code.

Note: development documentation is in process of being moved to Mozilla Development Center (MDC).

You can print information to JavaScript Console using the following method:

  1. Declare a variable (global if you're scripting your own window or your object's member if you're overlaying an existing window):
    var gConsoleService = Components.classes['@mozilla.org/consoleservice;1']
                        .getService(Components.interfaces.nsIConsoleService);

    (note, that you can use toJavaScriptConsole() function to open JavaScript console on startup)

  2. Define a function:
    function myExt_logMessage(aMessage) {
      gConsoleService.logStringMessage('My extension: ' + aMessage);
    }
  3. Use it like here:
    myExt_logMessage("Initialized.");

See also: Viewing dump() output, Using Console service.