The MIME type registered for XUL files is application/vnd.mozilla.xul+xml. When editing and using XUL files locally, you shouldn't need to worry about setting this on your computer; however, sometimes you may need to set the MIME type, such as when you host XUL files on a server. Chapter 12 provides additional information about how you can set the correct file type for your system.
The first thing required in a XUL document is the XML declaration.
<?xml version="1.0"?>
<label value="Getting Started" />
Like other markup vocabularies, XUL uses a namespace declaration to define the particular elements that may be included in a valid file. Example 2-2 shows a sample of the required namespace declaration. The namespace is an attribute of the root window element. The lack of any suffix on the XML namespace declaration (i.e., xmlns:xul) indicates that XUL is the default namespace for this file.
If you want to include XUL content in documents that use other types of markup, you need to declare more than one namespace. Common namespace declarations for getting other language elements into your code include HTML and RDF, but you can invent your own as well. If you wanted to put the button from Example 2-1 into a vanilla XML file, for example, you could place it into an XML document by using the xmlns:xul attribute, as shown in Example 2-3.
This file has three types of content: XUL, HTML, and customized markup called flies. When you use mixed namespaces, you have to prefix the XUL elements with xul: to distinguish them from markup in other namespaces, as with the xul:box and xul:button shown in Example 2-3.
Example 2-1 features some very common XUL elements. In this section, each element is dissected to show what it does and how it interacts with other elements. The <window> element is the root of individual primary XUL documents (in contrast to dialogs that pop up from windows, which can use <dialog> as the root, and XUL documents loaded within other XUL containers, which can use <page>).
As in HTML, the root element defines the document into which all elements are drawn, but in XUL, that document is a piece of an application interface and not a web page. We'll have more to say about the window and some of its features in the second example.
A <box> element that contains a <button> is inside the window in Example 2-1. Although you can use attributes on the window element to lay out and position window children, it's never a bad idea to use the <box> as a container, particularly when you add new layout to your document, such as rows of buttons, grids, tabs, or other elements that need to be arranged precisely within the space of the window. The box is the basic element for layout in XUL.
The align attribute on the box specifies that the children do not stretch and center themselves in the middle of the available space. If the box was omitted and there were multiple children of the root window, they would be laid out vertically by default, one under the other. This setting can be overridden by adding the orient attribute to <window> and giving it a value of "horizontal."
<?xml version="1.0"?> <!DOCTYPE window> <window xmlns:html="http://www.w3.org/1999/xhtml" xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"> </window>
![]() | As with any existing Mozilla functions referred to in this book, you can look up toOpenWindowByType by using the LXR web-based source code viewer, described in Appendix A and available at http://lxr.mozilla.org/. |
An id attribute is present on the <window> element. Using this attribute is not necessary to run the windows system, but it is a good idea to give each window a unique identifier because it makes nodes easier to find from script (see the DOM method getElementByID in Chapter 5 for information about how to get elements by identifier). This is how to set up an ID attribute:
<window xmlns:html="http://www.w3.org/1999/xhtml" xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" id="xflyMain">
Load event handlers such as onload and onunload are useful and necessary if you want to add behavior to a window, pass input to it, or manipulate its content depending on context:
<window xmlns:html="http://www.w3.org/1999/xhtml" xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" id="xfly-main" onload="startUp( )" onunload="shutdown( )" onclose="onClose( )">
When you load a XUL file that begins in this way, the event handler attributes onload and onunload carry out the functions listed as values (startUp( ) and shutdown( )). In addition, Mozilla provides an onclose event handler that intercepts the upcoming window closure to carry out any extra processing you need. The close event is fired before the unload event, so you can stop the window from closing in the onclose event handler if necessary. To stop window closure, the close event must return false.
Additional handlers are available for dialog windows. They are listed and their use is outlined in the section Section 3.2 in Chapter 3.
Navigator |
Document |
window |
Parent |
---|---|---|---|
Top |
Scrollbars |
name |
ScrollX |
ScrollY |
ScrollTo |
scrollBy |
GetSelection |
ScrollByLines |
ScrollByPages |
Size ToContent |
Dump |
SetTimeout |
SetInterval |
Clear Timeout |
ClearInterval |
SetResizable |
CaptureEvents |
Release Events |
RouteEvent |
Enable External Capture |
DisableExternal Capture |
prompt |
Open |
OpenDialog |
Frames |
find |
self |
Screen |
History |
content |
Sidebar |
Menubar |
Toolbar |
Locationbar |
Personalbar |
Statusbar |
Directories |
closed |
Crypto |
pkcs11 |
Controllers |
opener |
Status |
defaultStatus |
Location |
innerWidth |
InnerHeight |
outerWidth |
OuterHeight |
screenX |
ScreenY |
pageXOffset |
PageYOffset |
length |
FullScreen |
alert |
Confirm |
focus |
Blur |
back |
Forward |
home |
Stop |
|
MoveTo |
moveBy |
ResizeTo |
resizeBy |
Scroll |
close |
UpdateCommands |
escape |
Unescape |
atob |
Btoa |
AddEvent Listener |
RemoveEvent Listener |
Dispatch Event |
GetComputed Style |
Special properties of the XUL window object include:
Using this property is a quick way to access the content area of your window, if one exists. This property is useful only if your window uses one of the content elements, namely <iframe>, <browser>, and <editor>. Refer to the section Section 3.8 in Chapter 3 for a more detailed discussion. The content property is linked only to the frame you have explicitly declared as the primary area.
<browser type="content-primary" ...>
Subsequently, you can access and manipulate the content.
window.content.focus( );
This property is used to ensure intrinsic sizing, which is important in XUL application development, especially in dialog windows. Intrinsic sizing ensures that the window adapts and morphs to fit the content. This is preferable to constraining your window with a fixed width and height when the onload handler anticipates changeable content, depends on context, or takes input from another window. The colorpicker in the Mozilla Editor, for example, uses this function to make sure that the window displaying the chosen palette shrinks to fit that palette:
function ChangePalette(palette) { gDialog.ColorPicker.setAttribute("palettename", palette); window.sizeToContent( ); }
Chapter 8 provides full details of how to understand and use XPCOM components.
window.open (url, name, features);
window.openDialog (url, type, features, argument1, argument2);
Here is a list of some of the features of a XUL window opened using window.openDialog:
window.openDialog("chrome://xfly/content/utils/prompt.xul", "xFly_prompt", "chrome,dialog,modal", message);