Focus tab or window

前端 未结 11 728
慢半拍i
慢半拍i 2020-12-06 16:19

for a little app, I\'m opening a few windows/tabs from my script. Whether the browser opens a window or a tab is of course not in my hand.

However, I hold the refere

相关标签:
11条回答
  • 2020-12-06 16:54

    The following appears to work in IE8 and FF13:

    <script type="text/javascript">
    // Stupid script to force focus to an existing tab when the link is clicked.
    // And yes, we do need to open it twice.
    function openHelp(a) {
        var tab = window.open(a.href, a.target);
        tab.close();
        tab = window.open(a.href, a.target);
        return false;
    }
    </script>
    <a href="help.html" target="help" onclick="return openHelp(this);">Help</a>
    
    0 讨论(0)
  • See Mozilla's documentation: https://developer.mozilla.org/en/Code_snippets/Tabbed_browser.

      var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"]
                         .getService(Components.interfaces.nsIWindowMediator);
      var browserEnumerator = wm.getEnumerator("navigator:browser");
    
      // Check each browser instance
    
      while (browserEnumerator.hasMoreElements()) {
        var browserWin = browserEnumerator.getNext();
        var tabbrowser = browserWin.gBrowser;
    
        // Check each tab of this browser instance
        var numTabs = tabbrowser.browsers.length;
        for (var index = 0; index < numTabs; index++) {
          var currentBrowser = tabbrowser.getBrowserAtIndex(index);
          if (/*some logic*/) {
    
            // For an example
            tabbrowser.selectedTab = tabbrowser.tabContainer.childNodes[index];
    
            // Focus *this* browser-window
            browserWin.focus();
            break;
          }
        }
    

    Here is an easier event-driven approach - https://developer.mozilla.org/En/Listening_to_events_on_all_tabs.

    0 讨论(0)
  • 2020-12-06 17:00

    The only solution I see, is to force the popup in a new window, since there doesn't seem to be a way to focus another tab. This solution also requires you to change the default Javascript security settings in Tools > Options > Content tab and click on the Advanced button next to Enable Javascript checkbox and check the middle box to allow focusing windows.

    To force the use of a window rather than a tab, use win = window.open("http://www.google.com", "test" ,"modal=yes"); and then call win.focus(); whenever you feel like it.

    EDIT: Actually forgot to mention the fact that this is FF only.

    0 讨论(0)
  • 2020-12-06 17:00

    Have you tried in menu Tools -> Options… -> Content tab -> Advanced button next to "Enable JavaScript", to check the box named "Raise or Lower Windows" ?

    An example on how to set the focus can be found here.

    0 讨论(0)
  • 2020-12-06 17:01

    It is dirty and ugly, so it is only plus information: if you call an alert() in any of the open tabs/windows, that tab will gain focus.

    0 讨论(0)
提交回复
热议问题