Writing a Mozilla Application with XUL and Javascript
  Instantiating an XPCOM Component [ << Previous | Next >> ]

  • Instantiating an XPCOM component from JavaScript is as easy as...
    
    /* get the factory */
    var prefFactory =
        Components.classes["component://netscape/preferences"];
    /* create a default instance (nsISupports) */
    var prefInstance =
        prefFactory.createInstance();
    /* query the instance for the correct interface */
    var pref =
        prefInstance.QueryInterface(Components.interfaces.nsIPref);
  • Or, to put it more simply...
    
    const nsIPref = Components.interfaces.nsIPref;
    const PREF_PROGID = "component://netscape/preferences";
    var pref = Components.classes[PREF_PROGID].createInstance(nsIPref);
  • Both of the previous examples omitted error checks, it is generally a good idea to wrap component creation in a try / catch block.

[ Presentation Overview | Agenda | XUL Documents | Summary of XUL Elements | XUL Document Example | XUL Document Screenshot | XUL Overlays | XUL Overlay Example | XUL Overlay Screenshot | RDF Data | RDF Graph Illustration | RDF and XUL Templates | XUL Template Example | XUL Template Example, Points of Interest | Conclusions | Extra Slides | RDF Data Sources | XPCOM and XPConnect | XPConnect Objects | Instantiating an XPCOM Component | Chrome structure | Installing Chrome | Sample manifest.rdf ]