Enumerating tabbrowser tabs: Difference between revisions

From MozillaZine Knowledge Base
Jump to navigationJump to search
mNo edit summary
(cat redirects)
 
(13 intermediate revisions by 4 users not shown)
Line 1: Line 1:
{{extdev}}
Moved to [http://developer.mozilla.org/en/docs/Code_snippets:Tabbed_browser#Enumerating_tabs MDC]


==Enumerating tabbrowser tabs==
[[Category:Redirects]]
<ul><li>To go through all open tabs in a browser, you first need to get a reference to browser's window. If your code is executed from a browser.xul (in Firefox) overlay (for example it is a toolbar button or menu ''click'' handler), you can access current window via <tt>window</tt> pre-defined variable.
 
If your code is executed from its own window (for example, Settings dialog), you can  use [http://xulplanet.com/references/xpcomref/ifaces/nsIWindowMediator.html nsIWindowMediator] to get it (further info in [[nsIWindowMediator]]).</li>
 
<li>Then you need to get the <tabbrowser> element. You can get it with <tt>win.gBrowser</tt>, where <tt>win</tt> is the browser's window from the previous step. You can use just <tt>gBrowser</tt> instead of <tt>window.gBrowser</tt>, if running from browser.xul overlay.</li>
 
<li>Now use <tt>gBrowser.mPanelContainer.childNodes.length</tt> to get the number of open tabs. Then use <tt>gBrowser.getBrowserAtIndex()</tt> to get a <browser> element. For example:
<pre>var l = gBrowser.mPanelContainer.childNodes.length;
for(var i = 0; i < l; i++) {
  var b = gBrowser.getBrowserAtIndex(i);
  try {
    dump(b.currentURI.spec); // dump URLs of all open tabs to console
  } catch(e) {}
}</pre>
</li>
</ul>
 
To learn what methods are available for <browser> and <tabbrowser> elements, use [[DOM Inspector]] or look in [http://lxr.mozilla.org/seamonkey/source/toolkit/content/widgets/browser.xml browser.xml] and [http://lxr.mozilla.org/seamonkey/source/toolkit/content/widgets/tabbrowser.xml tabbrowser.xml] for corresponding [[Dev : Extensions : Resources#XBL|XBL bindings]].
 
[[Category:Development]]

Latest revision as of 05:38, 6 February 2007

Moved to MDC