B2G/gecko/browser/base/content/browser.js
/**
 * Switch to a tab that has a given URI, and focusses its browser window.
 * If a matching tab is in this window, it will be switched to. Otherwise, other
 * windows will be searched.
 *
 * @param aURI
 *        URI to search for
 * @param aOpenNew
 *        True to open a new tab and switch to it, if no existing tab is found.
 *        If no suitable window is found, a new one will be opened.
 * @return True if an existing tab was found, false otherwise
 */
Switch to a tab that has a given URI, and focusses its browser window.
If a matching tab is in this window, it will be switched to. Otherwise, other
windows will be searched.

@param aURI
URI to search for
@param aOpenNew
True to open a new tab and switch to it, if no existing tab is found.
If no suitable window is found, a new one will be opened.
@return True if an existing tab was found, false otherwise
Switch to a tab that has a given URI and focusses its browser window . If a matching tab is in this window it will be switched to . Otherwise other windows will be searched . @param aURI URI to search for @param aOpenNew True to open a new tab and switch to it if no existing tab is found . If no suitable window is found a new one will be opened . @return True if an existing tab was found false otherwise
function switchToTabHavingURI(aURI, aOpenNew) {
  function switchIfURIInWindow(aWindow) {
    let browsers = aWindow.gBrowser.browsers;
    for (let i = 0; i < browsers.length; i++) {
      let browser = browsers[i];
      if (browser.currentURI.equals(aURI)) {
        aWindow.focus();
        aWindow.gBrowser.tabContainer.selectedIndex = i;
        return true;
      }
    }
    return false;
  }
  if (!(aURI instanceof Ci.nsIURI))
    aURI = Services.io.newURI(aURI, null, null);
  let isBrowserWindow = !!window.gBrowser;
  if (isBrowserWindow && switchIfURIInWindow(window))
    return true;
  let winEnum = Services.wm.getEnumerator("navigator:browser");
  while (winEnum.hasMoreElements()) {
    let browserWin = winEnum.getNext();
    if (browserWin.closed || browserWin == window)
      continue;
    if (switchIfURIInWindow(browserWin))
      return true;
  }
  if (aOpenNew) {
    if (isBrowserWindow && isTabEmpty(gBrowser.selectedTab))
      gBrowser.selectedBrowser.loadURI(aURI.spec);
    else
      openUILinkIn(aURI.spec, "tab");
  }
  return false;
}
function switchToTabHavingURI ( aURI , aOpenNew ) { function switchIfURIInWindow ( aWindow ) { let browsers = aWindow . gBrowser . browsers ; for ( let i = 0 ; i < browsers . length ; i ++ ) { let browser = browsers [ i ] ; if ( browser . currentURI . equals ( aURI ) ) { aWindow . focus ( ) ; aWindow . gBrowser . tabContainer . selectedIndex = i ; return true ; } } return false ; } if ( ! ( aURI instanceof Ci . nsIURI ) ) aURI = Services . io . newURI ( aURI , null , null ) ; let isBrowserWindow = ! ! window . gBrowser ; if ( isBrowserWindow && switchIfURIInWindow ( window ) ) return true ; let winEnum = Services . wm . getEnumerator ( "navigator:browser" ) ; while ( winEnum . hasMoreElements ( ) ) { let browserWin = winEnum . getNext ( ) ; if ( browserWin . closed || browserWin == window ) continue ; if ( switchIfURIInWindow ( browserWin ) ) return true ; } if ( aOpenNew ) { if ( isBrowserWindow && isTabEmpty ( gBrowser . selectedTab ) ) gBrowser . selectedBrowser . loadURI ( aURI . spec ) ; else openUILinkIn ( aURI . spec , "tab" ) ; } return false ; }
Switch to a tab that has a given URI, and focusses its browser window.
Switch to a tab that has a given URI and focusses its browser window .
// This will switch to the tab in aWindow having aURI, if present. // Focus the matching window & tab // This can be passed either nsIURI or a string. // Prioritise this window. // Skip closed (but not yet destroyed) windows, // and the current window (which was checked earlier). // No opened tab has that url.