How to Quit a XUL Application

Script can attempt to quit a XUL application, or force the application to quit, using the nsIAppStartup interface.

<script>
function quit (aForceQuit)
{
  var appStartup = Components.classes['@mozilla.org/toolkit/app-startup;1'].
    getService(Components.interfaces.nsIAppStartup);

  // eAttemptQuit will try to close each XUL window, but the XUL window can cancel the quit
  // process if there is unsaved data. eForceQuit will quit no matter what.
  var quitSeverity = aForceQuit ? Components.interfaces.nsIAppStartup.eForceQuit :
                                  Components.interfaces.nsIAppStartup.eAttemptQuit;
  appStartup.quit(quitSeverity);
}
</script>

Calling this function if there is an uncaught exception, to force the application to quit:

<script>
try {
  doSomething();
}
catch (e) {
  quit(true);
}
</script>

The "Quit" menuitem should typically prompt the user if there is unsaved data:

<menuitem label="Quit" oncommand="quit(false);"/>