Writing XUL and Javascript

Friday, 17 Dec 2004

If you need or want to write any XUL code at all, it is highly advisable that you read the JSLib documentation and sources. Whether you want to depend on the library is another issue altogether, but the dearth of documentation on the interfaces that it wraps makes the source a prime resource for understanding how to work all the classes the XPCOM library in Mozilla has to offer. You also want to have a copy of the XUL Periodic Table (itself a XUL app) handy.

There’s lots of good documentation on the more trivial aspects of the formats and APIXULPlanet is the obvious first stop, but be sure to also read the MozillaZine Knowledgebase for Developers and to bookmark the Creating Applications with Mozilla book.The Open XUL Alliance has many links and a bunch of downloadable e-books on store.

To begin with, you want to read at least one of these tutorials:

You also want to be aware of the javascript.options.showInConsole and javascript.options.strict settings. Enable them to make life easier. Unfortunately, even Firefox’ own XUL Javascript isn’t strict clean, and most extensions are even less so, so prepare to sift through lots of noise in order to read your own debugging messages. The Javascript console really needs a filtering mechanism. (Maybe I should file a bug, hmm…)

And lastly, you’ll want to have this function on hand:

function _logconsole( msg ) {
  // not terribly efficient, just does the job
  Components
  .classes[ "@mozilla.org/consoleservice;1" ]
  .getService( Components.interfaces[ "nsIConsoleService" ] )
  .logStringMessage( msg );
}

With it you can dump debugging messages to the Javascript console, which greatly eases the pain of “printf debugger” cowboys.

Of course, the Extension Developer’s Extension makes all this much easier.

Happy hacking!