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
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>
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.
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.
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.
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.