Enumerating tabbrowser tabs: Difference between revisions

From MozillaZine Knowledge Base
Jump to navigationJump to search
m (remove unneeded heading)
m (<code> tags)
Line 1: Line 1:
{{extdev}}
{{extdev}}


<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.
<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 <code>browser.xul</code> (in Firefox) overlay (for example it is a toolbar button or menu ''click'' handler), you can access current window via <code>window</code> 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.</li>
If your code is executed from its own window (for example, Settings dialog), you can use <code>[http://xulplanet.com/references/xpcomref/ifaces/nsIWindowMediator.html nsIWindowMediator]</code> to get it.</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>Then you need to get the <code><tabbrowser></code> element. You can get it with <code>win.gBrowser</code>, where <code>win</code> is the browser's window from the previous step. You can use just <code>gBrowser</code> instead of <code>window.gBrowser</code>, if running from <code>browser.xul</code> 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:
<li>Now use <code>gBrowser.mPanelContainer.childNodes.length</code> to get the number of open tabs. Then use <code>gBrowser.getBrowserAtIndex()</code> to get a <code><browser></code> element. For example:
<pre>var l = gBrowser.mPanelContainer.childNodes.length;
<pre>var l = gBrowser.mPanelContainer.childNodes.length;
for(var i = 0; i < l; i++) {
for(var i = 0; i < l; i++) {
Line 18: Line 18:
</ul>
</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 [[Development resources#XBL|XBL]] bindings.
To learn what methods are available for <code><browser></code> and <code><tabbrowser></code> 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 [[Development resources#XBL|XBL]] bindings.


[[Category:Example code]] [[Category:Javascript example code]]
[[Category:Example code]] [[Category:Javascript example code]]

Revision as of 00:27, 6 March 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).

  • 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 window pre-defined variable. If your code is executed from its own window (for example, Settings dialog), you can use nsIWindowMediator to get it.
  • Then you need to get the <tabbrowser> element. You can get it with win.gBrowser, where win is the browser's window from the previous step. You can use just gBrowser instead of window.gBrowser, if running from browser.xul overlay.
  • Now use gBrowser.mPanelContainer.childNodes.length to get the number of open tabs. Then use gBrowser.getBrowserAtIndex() to get a <browser> element. For example:
    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) {}
    }

To learn what methods are available for <browser> and <tabbrowser> elements, use DOM Inspector or look in browser.xml and tabbrowser.xml for corresponding XBL bindings.