Home arrow XML arrow Page 2 - An Introduction to XUL Part 5

Buttons, JavaScript, and XPCOM - XML

Learn about XUL, a subset of XML used to describe user interfaces, that helps you to make rich user interfaces with nothing more complicated than a text editor. In the fifth part of this series you will receive an introduction to XPCOM, the object model used by XUL, and take a close look at a basic example of the combination of Mozilla components and interfaces that make up the abstraction layer.

  1. An Introduction to XUL Part 5
  2. Buttons, JavaScript, and XPCOM
  3. The nsIFilePicker interface
  4. The keyset element
By: Dan Wellman
Rating: starstarstarstarstar / 9
June 20, 2005

print this article



XUL does not give you as many different built-in buttons as other, more powerful programming languages such as Visual Basic or C, but you have to remember that XUL is just a mark-up language. Other buttons you can use are the help and disclosure buttons, which can be used by adding them to the comma delimited list in the dialog tag. Unlike the ok and cancel buttons, these will not do anything without adding scripts for them, but there are built-in methods (ondialoghelp and ondialogdisclosure) that will let you do this easily.

JavaScript is able to perform many of the processes that lie behind an application and is used to add behavior to menus, buttons and other elements. Unfortunately, there are many things an application will need to do that are beyond the reach of JavaScript, such as working with local files. To give XUL the power that it clearly has (after all, both Mozilla and Firefox are written entirely in XUL and these are world-class browser applications), Mozilla provides an object model that allows you to implement instances of existing components of Mozilla. These components are themselves composed of interfaces that contain specific sets of functionality, which allow you to do the kind of advanced things that applications need to do in order to function.

Programmers that have used .NET, for example, to inherit the functionality of pre-existing classes will feel right at home here. The object model used by Mozilla is XPCOM, which stands for cross platform component object model. In order to open and save files, for example, you will need to create an instance of a component that is able to access the required functions by implementing the correct interface, which will form the basis of the next example and will draw upon some of the elements we have already worked with.

You should only need a basic window to test the functionality of the file interface needed to produce an open file dialog. The window won’t actually open or save any files because this requires more components and interfaces and is beyond the scope of this article, but the code will demonstrate how to create a standard dialog box that people can use to select files for opening. The following window, which will need to be described by a contents.rdf and accessed through Mozilla should be enough:

<?xml version="1.0"?>
  title="Opening and Saving Files" width="500" height="300"

<menubar id="mainmenu" grippyhidden="true">
  <menu id="file" label="File" accesskey="f">
    <menupopup id="filepop">
      <menuitem label="Open" accesskey="o" />
      <menuitem label="Save" accesskey="s"/>
      <menuitem label="Exit" accesskey="e"


This will create a basic window with a basic menu in it. There are just three items in the menu, open, save and exit. There is almost no additional code required to close the window; just a simple JavaScript method that closes the current window. The oncommand event is used because we have specified accesskeys for the menu and items, which means that a user could select the exit menu item with the mouse or with the keyboard.

>>> More XML Articles          >>> More By Dan Wellman

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Google Docs and Xpath Data Functions
- Flex Array Collection Sort and Filtering
- The Flex Tree Control
- Flex List Controls
- Working with Flex and Datagrids
- How to Set Up Podcasting and Vodcasting
- Creating an RSS Reader Application
- Building an RSS File
- An Introduction to XUL Part 6
- An Introduction to XUL Part 5
- An Introduction to XUL Part 4
- An Introduction to XUL Part 3
- An Introduction to XUL Part 2
- An Introduction to XUL Part 1
- XML Matters: Practical XML Data Design and M...

Developer Shed Affiliates


Dev Shed Tutorial Topics: