We're going to be creating a simple find files utility throughout this tutorial.
First, however, we should look at the basic syntax of a XUL file.
An XUL file can be given any name but it really should have a .xul extension. The simplest XUL file has the following structure:
<?xml version="1.0"?> <?xml-stylesheet href="chrome://global/skin/" type="text/css"?> <window id="findfile-window" title="Find Files" orient="horizontal" xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"> <!-- Other elements go here --> </window>
This window will not do anything since it doesn't contain any UI elements. Those will be added in the next section. In fact, there will not be shown any window at all. If you want to force the window to become visible you can add the width
and height
attribute to the window
tag. Here is a line by line breakdown of the code above:
window
. Each user interface window is described in a separate file. This tag is much like the HTML tag which surrounds an entire HTML document, except that a user interface window is described instead of a document. Several attributes can be placed in the window
tag -- in this case there are four. In the example, each attribute is placed on a separate line but they do not have to be.id
attribute is used as an identifier so that the window can be referred to by scripts. You will usually put an id
attribute on all elements. The name can be anything you want although it should be something relevant.title
attribute describes the text that would appear on the title bar of the window when it is displayed. In this case the text 'Find Files' will appear.orient
attribute specifies the arrangement of the items in the window. The value horizontal
indicates that items should be placed horizontally across the window. You may also use the value vertical
, which means that the items are placed in a column. This is the default value, so you may leave the attribute off entirely if you wish to have vertical orientation.window
tag at the end of the file.In order to open a XUL window, there are several methods that can be used. If you are only in the development stage, you can just type the URL (whether a chrome:, file: or other URL type) into the location bar in a Mozilla browser window. You should also be able to double-click the file in your file manager, assuming that XUL files are associated with Mozilla. The XUL window will appear in the browser window as opposed to a new window, but this is often sufficient during the early stages of development.
Note: The local XUL document can be opened in the browser as mentioned above only if |dom.allow_XUL_XBL_for_file| preference in "about:config" has been set to |true| . If it does not exist, then it needs to be created and set to |true|.
The correct way, of course, is to open the window using JavaScript. No new syntax is necessary as you can use the window.open()
function as one can do for HTML documents. However, one additional flag, called 'chrome' is necessary to indicate to the browser that this is a chrome document to open. This will open the window without the toolbars and menus and so forth that a normal browser window has. The syntax is described below:
window.open(url,windowname,flags);
where the flags contains the flag "chrome" as in this example
window.open("chrome://navigator/content/navigator.xul", "bmarks", "chrome,width=600,height=300");
If you are using Firefox, try below:
window.open("chrome://browser/content/places/places.xul", "bmarks", "chrome,width=600,height=300");
You can test lines of JavaScript like these in the Error Console. Choose Tools – Error Console, type a line of JavaScript, and press the Evaluate button, or the Return or Enter key.
Let's begin by creating the basic file for the find file dialog. Create a file called findfile.xul and put it in the content directory specified in the findfile.manifest file (we've created in the previous section). Add the XUL template shown at the top of this page to the file and save it.
You can use the command-line parameter '-chrome' to specify the XUL file to open when Mozilla starts. If this is not specified, the default window will open. (Usually the browser window.) For example, we could open the find files dialog with either of the following:
mozilla -chrome chrome://findfile/content/findfile.xul mozilla -chrome resource:/chrome/findfile/content/findfile.xul
If you run this command from a command-line (assuming you have one on your platform), the find files dialog will open by default instead of the Mozilla browser window. Of course, because we haven't put any UI elements in the window, you won't see a window appear. We'll add some elements in the next section.
To see the effect though, the following will open the bookmarks window:
mozilla -chrome chrome://communicator/content/bookmarks/bookmarksManager.xul
If you are using Firefox, try below.
firefox -chrome chrome://browser/content/places/places.xul
The '-chrome' argument doesn't give the file any additional privileges. Instead, it causes the specified file to open as a top-level window without any browser chrome, such as the address field or menu. Only chrome URLs have additional privileges.
<?xml-stylesheet href="chrome://global/skin/" type="text/css"?>
In the next section, we will add some buttons to the window.