NsIWindowMediator: Difference between revisions

From MozillaZine Knowledge Base
Jump to navigationJump to search
(moved to devmo)
 
(3 intermediate revisions by one other user not shown)
Line 1: Line 1:
{{extdev}}
See [http://developer.mozilla.org/en/docs/nsIWindowMediator nsIWindowMediator] at the Mozilla Developer Center.
{{wrongtitle|title=nsIWindowMediator}}


Window mediator is Mozilla component that keeps track of open windows. It's accessed through <code>nsIWindowMediator</code> interface. Two most common uses of nsIWindowMediator are
[[Category:Redirects]]
# Getting the most recent / any window of given type.
# Enumerating all windows of given type.
 
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 ==
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>
var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"].
        getService(Components.interfaces.nsIWindowMediator);
var win = wm.getMostRecentWindow(type);
</pre>
 
== 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 ==
* [http://xulplanet.com/references/xpcomref/ifaces/nsIWindowMediator.html nsIWindowMediator reference page on XUL Planet]
 
[[Category:Example code|nsIWindowMediator]] [[Category:XPCOM example code|nsIWindowMediator]]

Latest revision as of 21:00, 19 September 2006

See nsIWindowMediator at the Mozilla Developer Center.