MozillaZine

NsIWindowMediator

From MozillaZine Knowledge Base

(Difference between revisions)
Revision as of 00:16, 28 April 2005
Asqueella (Talk | contribs)

<-- Previous diff
Revision as of 03:54, 1 August 2005
Asqueella (Talk | contribs)
(Getting most recent window)
Next diff -->
Line 11: Line 11:
== Getting most recent window == == Getting most recent window ==
 +The following code is useful when you need any of the windows of given type, or to check if a window of a particular type (e.g. your extension's Options dialog) is already open.
 +
 +<code>getMostRecentWindow</code> returns a <code>Window</code> object, or <code>null</code>, if there are no windows with given <code>windowtype</code> open.
<pre> <pre>
var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"]. var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"].

Revision as of 03:54, 1 August 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

The following code is useful when you need any of the windows of given type, or to check if a window of a particular type (e.g. your extension's Options dialog) is already open.

getMostRecentWindow returns a Window object, or null, if there are no windows with given windowtype open.

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