mozilla

Revision 178160 of How To Pass an XPCOM Object to a New Window

  • Revision slug: How_To_Pass_an_XPCOM_Object_to_a_New_Window
  • Revision title: How To Pass an XPCOM Object to a New Window
  • Revision id: 178160
  • Created:
  • Creator: Nickolay
  • Is current revision? No
  • Comment fix source code link

Revision Content

A more useful example is available in the source code: {{ Source("toolkit/components/help/content/contextHelp.js#61") }}

If you want to be able to call functions within an XPCOM object from a XUL window's code, you can do so if you pass the XPCOM object as one of the arguments to the window creation method.

For example:

var ww = 
      Components.classes["@mozilla.org/embedcomp/window-watcher;1"].
            getService(Components.interfaces.nsIWindowWatcher);

var win = ww.openWindow(null,
      "chrome://myextension/content/debug.xul",
      "debug history", "chrome,centerscreen,resizable", myObject);

Note in this example that myObject is passed to the openWindow() method; you can pass any XPCOM object (or any other value, for that matter) in this way. To access the XPCOM object from the window's code, you can access the window.arguments[] array, as shown in the example below:

Components.utils.reportError(String(window.arguments[0]));

This will produce output similar to "{{ mediawiki.external('xpconnect wrapped nsIMyXPCOMObject') }}".

Revision Source

<div class="note">
<p>A more useful example is available in the source code: {{ Source("toolkit/components/help/content/contextHelp.js#61") }}
</p>
</div>
<p>If you want to be able to call functions within an XPCOM object from a XUL window's code, you can do so if you pass the XPCOM object as one of the arguments to the window creation method.
</p><p>For example:
</p>
<pre>var ww = 
      Components.classes["@mozilla.org/embedcomp/window-watcher;1"].
            getService(Components.interfaces.nsIWindowWatcher);

var win = ww.openWindow(null,
      "chrome://myextension/content/debug.xul",
      "debug history", "chrome,centerscreen,resizable", myObject);
</pre>
<p>Note in this example that <code> myObject </code> is passed to the <code>openWindow()</code> method; you can pass any XPCOM object (or any other value, for that matter) in this way. To access the XPCOM object from the window's code, you can access the <code>window.arguments[]</code> array, as shown in the example below:
</p>
<pre>Components.utils.reportError(String(window.arguments[0]));
</pre>
<p>This will produce output similar to "<code>{{ mediawiki.external('xpconnect wrapped nsIMyXPCOMObject') }}</code>".
</p>
Revert to this revision