MozillaZine

Dev : Tips : Printing to JSConsole

From MozillaZine Knowledge Base

(Difference between revisions)
Revision as of 19:55, 10 July 2004
Asqueella (Talk | contribs)

<-- Previous diff
Revision as of 22:34, 10 July 2004
Asqueella (Talk | contribs)
(toJavaScriptConsole)
Next diff -->
Line 5: Line 5:
*Initialize it [[Dev : Extensions : onLoad|on load]]: *Initialize it [[Dev : Extensions : onLoad|on load]]:
aConsoleService = Components.classes['@mozilla.org/consoleservice;1'].getService(Components.interfaces.nsIConsoleService); aConsoleService = Components.classes['@mozilla.org/consoleservice;1'].getService(Components.interfaces.nsIConsoleService);
 +(note, you can also use toJavaScriptConsole() function to open JavaScript console on startup)
*Define a function: *Define a function:
function logMessage(message) { function logMessage(message) {
Line 11: Line 12:
*Use it like here: *Use it like here:
logMessage('Initialized.'); logMessage('Initialized.');
 +
 +(thanks to clav, velcrospud and sage.mozdev.org for this)
See also: [[Dev : Tips : Enabling dump|Using dump()]] See also: [[Dev : Tips : Enabling dump|Using dump()]]

Revision as of 22:34, 10 July 2004

You can output information to console using the following method:

  • Declare global variable aConsoleService:
var aConsoleService;
aConsoleService = Components.classes['@mozilla.org/consoleservice;1'].getService(Components.interfaces.nsIConsoleService);

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

  • Define a function:
function logMessage(message) {
  aConsoleService.logStringMessage('MyApp: ' + message);
}
  • Use it like here:
logMessage('Initialized.');

(thanks to clav, velcrospud and sage.mozdev.org for this)

See also: Using dump()