# Preface

Mozilla is not just a web browser. It is also a framework for building cross-platform applications using standards such as Cascading Style Sheets (CSS), XML languages such as the XML-based User-interface Language (XUL), eXtensible Binding Language (XBL), and Resource Description Framework (RDF).

Gecko, Mozilla's rendering engine, is used as part of the framework, along with other technologies such as XPConnect and XPCOM, Mozilla's component model. The Mozilla development framework also uses programming languages such as JavaScript, C++, C, Python, and Interface Definition Language (IDL).

The Mozilla framework is used to create Netscape's Mozilla-based browsers (Netscape 6.x and 7.x), other browsers such as Galeon and Chimera, and chat clients like ChatZilla and JabberZilla. Developers also use Mozilla to create development tools, browser enhancements, games, and other types of add-ons and applications.

This book explains how applications are created with Mozilla and provides step-by-step information that shows how to create your own programs using Mozilla's powerful cross-platform development framework. It also includes examples of different existing applications to demonstrate the possibilities of Mozilla development.

## Mozilla Background

When Netscape Communications Corporation was founded, it planned to create a better version of NCSA's Mosaic browser, the first application that made accessing the Internet possible for ordinary users. This new application would be a Mosaic Killer. In time, the word "Mozilla" became the shortened version of this phrase and the code word for Netscape's browsers.

Mozilla has become more than a reference to one of Netscape's products. On March 31, 1998, http://www.mozilla.org/ was launched as the site where the development of Netscape's next-generation Communicator 5.0 browser suite would take place. At that point, Mozilla became an open source project and began to take on a life of its own beyond its origins at Netscape.

When Netscape released its Communicator code to the open source community, it did something that was never done before: no other major software company had given away the source code to a proprietary product. At the time, many people in the software industry and the press debated the wisdom of this decision.

Many other companies have followed Netscape's lead and released their own products to the open source community. Sun Microsystems sponsors several projects, including http://www.openoffice.org/ and http://www.netbeans.org/. Apple also bases the core of its new operating system on an open source project called Darwin, hosted at http://developer.apple.com/darwin/.

A year after the Mozilla source code was released, Mike Homer, a senior executive at Netscape, made the following comments: "Mozilla is larger than Netscape, and that was its intention. Mozilla is essentially a collaborative project that was sponsored by a commercial entity. Some of the people that staff mozilla.org are Netscape employees, and the code that was contributed was code previously owned by Netscape. However, it's also true that the code base will take on a life of its own someday."

Since the project's launch, many people outside Netscape have joined the community, although many Netscape (now AOL) employees still contribute to its advancement. The Mozilla community is growing beyond the original home of mozilla.org. Other community resources worth examining include http://www.mozillazine.org/, an advocacy and news site, and http://www.mozdev.org/, a project-hosting site for Mozilla applications.

Several companies, including IBM, Red Hat, ActiveState, and Sun Microsystems have also contributed to the Mozilla community. For instance, Red Hat has provided support for Mozilla because it wants to help drive development of an open source alternative to the closed source Netscape 4.x browser suite that they had included in their Linux distribution. AOL has also explored the use of Mozilla in its latest CompuServe and AOL clients.

Because all Mozilla source code is made available to anyone who is interested, the community benefits from an increase in the number of suggestions, bug reports, patches, and new developers. Along with the people who were involved with the project when it was first released as open source, the new people and companies that joined the community have helped shape the direction and outcome of the Mozilla project.

## The State of Mozilla

Mozilla 1.0 was released on June 5, 2002, after more than four years of development as an open source project. This book was written so that all examples will work with this release and any 1.0.x maintenance release.

After the 1.0 release, two main development branches of Mozilla were created. The stable, long-lived 1.0 branch is dedicated to fixing bugs in the code of the 1.0 release. From this branch, periodic maintenance releases are labeled as Version 1.0.x. Every 1.0.x release is designed to be fully compatible with (though less buggy than) the original 1.0 release.

The other development branch is from the Mozilla CVS trunk. New releases from this development effort are labeled as 1.x and may include new features, changes to architecture, or other additions that help Mozilla evolve as a project.

These new 1.x releases may not be fully compatible with applications created to work with Mozilla 1.0 and the 1.0.x releases, but mozilla.org made a commitment to preserve frozen API compatibility (including XUL and XBL syntax) throughout the 1.x series until a future 2.0 release. See http://www.mozilla.org/roadmap/mozilla-1.0.html for details.

Because Mozilla itself is under active development, applications built on the framework may be affected when new versions of Mozilla are released. We recommend that you refer to mozilla.org's development road map for the latest information about the state of Mozilla; see http://www.mozilla.org/roadmap.html.

We also recommend that you use Mozilla 1.0.x versions when working with examples in this book. We encourage you to use the latest 1.x release as well so you can stay involved with the latest and greatest that Mozilla has to offer.

## Who Should Read This Book

This book is primarily aimed at programmers (and would-be programmers) interested in exploring this brand-new platform-the Mozilla development framework. However, you do not need to be a professional programmer to create your own cross-platform Mozilla-based applications.

As shown in the coming chapters, all you need to get started is a basic understanding of a few technologies that are already familiar to most web developers: CSS, XML, and JavaScript. In fact, this is one of the great advantages to developing a Mozilla-based application: the learning curve isn't as steep as most alternatives, such as C, C++, or even Java.

Your applications will be cross-platform automatically (although you can create platform-specific applications as well) and easily installable over the Internet by anyone running Mozilla on their computer. What more could you ask for in a development platform?

This book assumes that the reader has some level of familiarity with JavaScript, CSS, HTML, and XML. Reading this book in conjunction with other books that are devoted specifically to these topics may be useful if you are not already comfortable using these technologies. Some useful O'Reilly & Associates titles include JavaScript: The Definitive Guide, Cascading Style Sheets: The Definitive Guide,HTML & DHTML: The Definitive Guide, Learning XML, and XML in a Nutshell, Second Edition. Concepts and technologies that are new to Mozilla or used with Mozilla in a new way are explained in detail throughout the book.

This book also assumes that the reader has access to a computer with Mozilla 1.0 or later installed on it, plus any text editor or word processor. Mozilla runs on almost any type of personal computer available today, so finding a compatible platform shouldn't be difficult. The full system requirements for any Mozilla release can be found on the mozilla.org site.

## Platform and Applications

Some developers work on Mozilla to improve the way it functions and other developers use Mozilla to create new applications. These two approaches reflect the dual nature of Mozilla as a development project and a framework for creating applications, but the line between the two isn't always clear.

People often start developing an application with Mozilla and then notice a way to make Mozilla itself work better, which will in turn make their application work better. In these cases, the developer works on both Mozilla applications and the Mozilla development framework that provides the plumbing those applications run on top of.

This distinction between platform and applications is important. This book provides in-depth information about application development using Mozilla, but it does not directly describe anything relating to the development of Mozilla itself.

If you are interested in learning how to become a Mozilla developer (and actually hacking the code), we can suggest a couple of starting points. Getting Your Work Into Mozilla is an article written by two authors who chronicled their own experiences about becoming a part of the Mozilla community, and is available at http://www.oreillynet.com/pub/a/mozilla/2000/09/29/keys.html. There is also great information about getting started with development on Mozilla from the mozilla.org site at http://www.mozilla.org/get-involved.html.

## Structure of the Book

This book is structured so you can create and distribute a simple Mozilla application after reading through Chapter 6. You should read Chapters 1-6 in order and work through the examples. The later chapters provide information about advanced aspects of application development that allow you to take full advantage of what Mozilla has to offer. If you wish, you can read Chapters 7-12 out of order, depending on your needs and interests.

Chapter 1, Mozilla as Platform, provides an introduction to Mozilla and its advantages as a development framework. Conceptually, the chapter gives you a thorough understanding of how JavaScript, CSS, and XUL are used to create cross-platform applications.

Chapter 2, Getting Started, goes into the initial technical details about creating Mozilla applications. Two "Hello World" examples are presented to show you how to build an application shell.

Chapter 3, XUL Elements and Features, introduces the XML-based User-interface Language, shows you how to create custom XUL documents, and explains how to use the XUL tag set to build out your application shell.

Chapter 4, CSS in Mozilla Applications, shows how Cascading Style Sheets are used to create the look and feel of an application and how custom CSS files work together with XUL files in your application shell.

Chapter 5, Scripting Mozilla, describes how to add functionality to your application by using JavaScript and how custom JS files added to your application shell interact with existing XUL and CSS files.

Chapter 6, Packaging and Installing Applications, explains how to package an application so it can be installed on different computers. Once you create your custom XUL, CSS, and JS files, you can package and distribute them to users as your Mozilla application after reading this chapter.

Chapter 7, Extending the UI with XBL, is the first of the more advanced chapters that show you how to extend the basic application framework described in Chapters 1-6. This chapter explains how to use the eXtensible Binding Language to create reusable widgets that will help organize a complicated application.

Chapter 8, XPCOM, examines Mozilla's cross-platform component object model. After reading this chapter, you should be able to find and use existing scriptable components in your own application and create a simple XPCOM component using JavaScript or C++.

Chapter 9, XUL Templates, explains how to include dynamic information in your application that can be updated from a database, user input, or other sources. XUL templates rely on RDF, but it is not necessary to fully understand RDF to be able to use XUL templates.

Chapter 10, RDF, RDF Tools, and the Content Model, looks more closely at how Mozilla configures and uses datasources with the Resource Description Framework. RDF is used in XUL templates, but it is also used throughout Mozilla and Mozilla applications. Although it is considered to be one of the most difficult technologies to learn and use, RDF is worth the effort.

Chapter 11, Localization, shows how to make your application more accessible by localizing it into any number of different languages. This chapter shows you how to use Document Type Definitions and string bundles to provide translations of your application's interface and other content, such as the Help documents you may provide in your application.

Chapter 12, Remote Applications, explores an alternate distribution method that has several advantages over the direct installation method discussed in Chapter 6. By serving your application from a server, you may be able to add extra features that wouldn't be possible otherwise.

The book's three appendixes contain supplemental information not directly applicable to any of the different chapters. Appendix A, Getting and Building the Mozilla Source, explains how to start using the Mozilla source code. Appendix B, Development Tools, describes projects that will assist you with your application development. Appendix C, Programmer's Reference, provides a convenient reference useful for quickly locating specific XUL and XBL elements and event attributes.

This book was written so that a reader who does not want to read about each aspect of application creation can read just the chapters that interest them and still get useful information. For instance, a graphic designer can read the chapters about XUL and CSS and learn how to create an application interface to which a programmer can then add functionality.

## How This Book Was Written

This book was created as a collaborative project hosted at http://books.mozdev.org. The authors of the book have become aware of the benefits of creating a project in an open source method while working within the Mozilla community and wanted to extend these benefits to the creation of the book itself.

The content of this book was released under the Open Publication License (OPL) and is available at the site mentioned in the previous paragraph and at http://www.oreilly.com/catalog/mozilla/. Anyone interested in contributing to the further development of this book is encouraged to visit the site; they can help us make sure that this book lives on as an active document that will continue to be useful to the Mozilla community.

The examples used throughout the book contain code fragments that were created specifically as examples to highlight the use of a particular aspect of Mozilla application development. Several existing Mozilla applications are also used as examples and more information about each is provided in the chapters where they are used. All numbered examples in the book can be found online at http://books.mozdev.org/examples/ and http://www.oreilly.com/catalog/mozilla/.

## Mozilla Licensing Information

The Mozilla source code is all publicly available and released under several different licenses. The details of Mozilla's licensing scheme aren't discussed here, but we provide information and pointers to where additional information can be found.

Several licenses are used in conjunction with the Mozilla source code, including the Mozilla Public License (MPL), Netscape Public License (NPL), GNU General Public License (GPL), and the GNU Lesser General Public License (LGPL). This wide selection of licenses was chosen to make the source available to as large a group of developers as possible.

Any code that is checked into the Mozilla source code tree needs to comply with mozilla.org's licensing policy. Because Mozilla applications can be created and stored anywhere, however, there are many different options for how to license and distribute a Mozilla-based application. One option is to sell your application with or without making all of the source available.

For more information about Mozilla's licensing scheme and the license options available to Mozilla applications, visit mozilla.org's license page at http://www.mozilla.org/MPL/. Questions about licensing issues can also be answered by posting to the license newsgroup netscape.public.mozilla.license or the license mailing list mozilla-license@mozilla.org.

## Conventions

The following conventions are used in this book:

`Constant Width` is used for:

- Code examples and fragments
- Anything that might appear in an HTML document, including element names, tags, attribute values, entity references, and processing instructions
- Anything that might appear in a program, including keywords, operators, method names, class names, commands, and literals

`Constant-Width Bold` is used for:

- User input in code examples
- To highlight certain code in examples

Italics is used for:

- New terms where they are defined
- Host and domain names (http://www.mozdev.org/)
- URLs (http://books.mozdev.org/index.html)
- Interface names and variable names

`Constant Width Italic` is used for replaceable items, such as variables or optional elements, within syntax lines of code.

You should pay special attention to notes set apart from the text with the following icons:

NOTE

This is a tip, suggestion, or a general note. It contains useful supplementary information about the topic at hand.

WARNING

This indicates a warning or caution. It will help you solve and avoid annoying problems.

Significant code fragments, complete programs, and documents are generally placed into a separate paragraph like this:

    <?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>

## Comments and Questions

Please address comments and questions concerning this book to the publisher:

O'Reilly & Associates, Inc. 1005 Gravenstein Highway North Sebastopol, CA 95472 1-800-998-9938 (in the United States or Canada) 1-707-829-0515 (international/local) 1-707-829-0104 (fax)

There is a web page for this book, which lists errata, examples, or any additional information. You can access this page at:

http://www.oreilly.com/catalog/mozilla

To comment or ask technical questions about this book, send email to:

bookquestions@oreilly.com

For more information about books, conferences, Resource Centers, and the O'Reilly Network, see the O'Reilly web site:

http://www.oreilly.com/

## Acknowledgments

This book was made possible by the help and contributions of many different people. Laurie Petrycki and Paula Ferguson are the editors at O'Reilly who helped create and shape this book. Their guidance and feedback has been crucial throughout this entire process. We would also like to thank Ann Schirmer, Mary Brady, Betsy Waliszewski, and David Chu at O'Reilly for their work on helping to create and promote this book.

Many other people have contributed their suggestions, ideas, and feedback, including Martin Kutschker, Andreas Otte, Andy Edmonds, Mike Potter, Chris Waterson, Mark Murphy, Pavol Vaskovic, Andrew Wooldridge, Doug Turner, Tao Cheng, Michael Ang, Neil Rashbrook, Rob Ginda, Steve Rudman, Kathleen Brade, Zach Lipton, Cameron Barrett, Chiko Chow, Jan Varga, Axel Hecht, David Ascher, Michael Gannon, David McNamara, and Mark Hammond.

Several Mozilla application developers have allowed us to use their projects in the book as examples. We would like to thank Rob Ginda for allowing us to feature his IRC client ChatZilla, his JavaScript Debugger Venkman and his XULKit development tools, David Gillen for his Snake game, Neil Rashbrook for his XULMine game, Andy Edmonds and Pavol Vaskovic for the Optimoz and Mouse Gestures projects, Henrik Lynggaard for MozillaTranslator, David Hyatt and Mike Pinkerton for the Chimera browser, and Gervase Markham for letting us reference Patch Maker.

We would like to thank Joshua Lerner, who helped with the creation of the book's original plans and also helped as a reviewer (Joshua and Kerry Fitzpatrick also let a couple of their employees at Alphanumerica work on Mozilla projects during their day job and funded the first Mozilla Developer Conference). We would like to thank Julia Kleyman for creating several of the illustrations used throughout the book, including the xFly logo (not to mention the FruityGum skin). Thanks also to Eoin Campbell for supporting Mozilla developers and having the vision to use Mozilla for application development from its very early days.

We would also like to thank the people who have supported us during the time this book was written, including Kim Steckler, Rachel Oeschger, and Priscilla, Dylan, and Devon Collins.