MozillaZine

NsIWindowMediator

From MozillaZine Knowledge Base

(Difference between revisions)
Revision as of 23:59, 27 April 2005
Asqueella (Talk | contribs)

<-- Previous diff
Revision as of 00:13, 28 April 2005
Asqueella (Talk | contribs)

Next diff -->
Line 5: Line 5:
# Getting the most recent / any window of given type. # Getting the most recent / any window of given type.
# Enumerating all windows of given type. # Enumerating all windows of given type.
-Examples below.+ 
 +In the examples below <code>type</code> specifies the type of windows you want to search. You can specify a type of your own window by creating an <code>windowtype</code> attribute on the <code><window></code> element.
 + 
 +Browser windows have <code>navigator:browser</code> window type. To search all windows, regardless of their type, pass an empty string, <code>""</code>.
== Getting most recent window == == Getting most recent window ==
 +<pre>
 +var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"].
 + getService(Components.interfaces.nsIWindowMediator);
 +var win = wm.getMostRecentWindow(type);
 +</pre>
== Enumerating windows == == Enumerating windows ==
- +<pre>
 +var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"].
 + getService(Components.interfaces.nsIWindowMediator);
 +var enumerator = wm.getEnumerator(type);
 +while(enumerator.hasMoreElements()) {
 + var win = enumerator.getNext();
 + // do something with |win|
 +}
 +</pre>
== References == == References ==
* [http://xulplanet.com/references/xpcomref/ifaces/nsIWindowMediator.html nsIWindowMediator reference page on XUL Planet] * [http://xulplanet.com/references/xpcomref/ifaces/nsIWindowMediator.html nsIWindowMediator reference page on XUL Planet]

Revision as of 00:13, 28 April 2005

This page is part of the extension development documentation project.

Ask your questions in MozillaZine Forums. Also try browsing example code.

Note: development documentation is in process of being moved to Mozilla Development Center (MDC).

Window mediator is Mozilla component that keeps track of open windows. It's accessed through nsIWindowMediator interface. Two most common uses of nsIWindowMediator are

  1. Getting the most recent / any window of given type.
  2. Enumerating all windows of given type.

In the examples below type specifies the type of windows you want to search. You can specify a type of your own window by creating an windowtype attribute on the <window> element.

Browser windows have navigator:browser window type. To search all windows, regardless of their type, pass an empty string, "".

Getting most recent window

var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"].
         getService(Components.interfaces.nsIWindowMediator);
var win = wm.getMostRecentWindow(type);

Enumerating windows

var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"].
         getService(Components.interfaces.nsIWindowMediator);
var enumerator = wm.getEnumerator(type);
while(enumerator.hasMoreElements()) {
  var win = enumerator.getNext();
  // do something with |win|
}


References