22

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 references to the newly created window objects and I do change their content "remotely" from another window. This all happens under the same document.domain so no xss problem.

The problem is, I cannot reliably focus those created windows/tabs. Since I'm writing a very specific app for a customer, I'm only targeting Firefox as browser. One option I have is of course just to do a remoteWindow.alert('foobar'); to get bring that window/tab up front, but that is pretty ugly isn't it.

I found this answer How to focus window/tab like alert()?

and it's said there, that Firefox has an option to allow script focus. So finally my question is, what is that option ? I searched the about:config for "tabs" and "focus" but didn't find anything related.

How to configure ?

Community
  • 1
  • 1
jAndy
  • 231,737
  • 57
  • 305
  • 359
  • 1
    If the browser disables focus I can't see any way around, except closing the window and reopening it, maybe preserving the user data if any is present. – Shadow The GPT Wizard Nov 17 '11 at 14:08
  • I think you are trying to break the limits the browsers put to the freedom of a page. If there is a solution, it is indeed not on by default. – Dykam Nov 17 '11 at 14:47
  • I'm pretty sure its not default (as a frontend developer, I hope so!) but I'd like to know if there is any option in Firefox, that overwrites the default behavior by allowing a script to change focused window/tab. – jAndy Nov 17 '11 at 14:54
  • It looks like the best way to do that (provided you can tell your users to enable/install/do stuff you want) is to create small firefox/chrome extension that would expose tab focusing function to javascript for your website. – WTK Nov 23 '11 at 10:16
  • 2
    Opening new browser windows/tabs is problematic. Have you considered [jqueryui](http://jqueryui.com/demos/dialog/) dialogs instead of opening a new window? – Ender Nov 22 '11 at 07:45

11 Answers11

14

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>
Alex
  • 151
  • 1
  • 3
  • This works pretty well! Thank you for sharing. It also works in Safari. However Chrome sees this as a popup and blocks it (and gives you the option to 'allow' and remember).Otherwise, a great option for loading a cms backend in another tab for the site moderator. – Jules May 25 '16 at 13:45
  • Despite this not working in mobile Safari, it's great nonetheless. – Brian Bobek Oct 05 '16 at 21:21
8

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.

zatatatata
  • 4,761
  • 1
  • 20
  • 41
  • Which browser enables you to set those options? – WTK Nov 23 '11 at 09:36
  • @WTK: Firefox does. However, Vahur: I tried but it does not work for tabs at least. When that option is set and you call `window.focus()` within some tab, its not getting the browser focus. – jAndy Nov 23 '11 at 09:52
  • @jAndy I stressed in the answer that it's impossible with tabs in Javascript. Either force the popup as a new window or write a plugin that enables tab focusing. – zatatatata Nov 23 '11 at 10:07
3

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.

Kirill Dubovikov
  • 1,457
  • 2
  • 21
  • 41
  • 2
    Thanks for the response, but I'm not writting a browser plugin. I was asking for a vanilla js solution within Firefox. More precisely for the option which allows the vanilla js to work. – jAndy Nov 15 '11 at 12:41
2

Have you tried window.focus()?

pradeek
  • 21,445
  • 2
  • 31
  • 32
  • Hmmm, the only way I can think of solving this would be to open each page in a seperate window. That would require you to set an option in Firefox to open each link and popup in a new window. – pradeek Nov 18 '11 at 10:52
2

There's no real way to do this for the simple reason that if websites had this ability, they'd end up using it to automatically focus ads or whatnot.

It can be done, but only in Firefox IF the user has it enabled, which he most likely won't.

skimberk1
  • 2,064
  • 3
  • 21
  • 27
  • How do you propose to do that in Firefox? – WTK Nov 22 '11 at 06:56
  • @WTK: good question. skimberk1: if you just describe how it is possible in firefox, you got the bounty. As I said, that wepapp is not for public audiance. – jAndy Nov 22 '11 at 09:11
2

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.

harrymc
  • 1,069
  • 8
  • 33
1

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.

rhinoeli
  • 151
  • 7
  • The solution I'm using has this as a fallback: `if(targetWindow.focus) targetWindow.focus(); setTimeout(function(){ if(!targetWindow.document.hasFocus()) targetWindow.alert('you rang...'); },500);` – Jon Hulka Feb 03 '15 at 16:27
1

To answer your question: I've found http://www.gtalbot.org/FirefoxSection/Popup/PopupAndFirefox.html#RaiseLowerSetting, which says the setting you're looking for is called "allow raise and lower windows".

Another setting would be about:config/dom.disable_window_flip (to false), found here.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • thanks for the reply. Unfortunately, the `about:config` setting is just reflecting the first option you mentioned (setable in the extended javascript options). Even more unfortunate, it will have no effect on browser tabs. I guess its just not possible. – jAndy Nov 24 '11 at 00:35
0

Expanding on Alex's contribution above. The use case is having a CMS. When a site owner is logged into the CMS, and the front end is loaded in another tab. You give the site owner a link on the front end that reloads the CMS tab and focuses is.

Place this javacript on front end pages (or include in a global JS file):

<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 loadAdmin(a) {
    var tab = window.open(a.href, a.target);
    tab.close();
    tab = window.open(a.href, a.target);
    return false;
}
</script>

I like to put the CMS link just after the H1 content. Again, this is only if they are logged into the CMS. In this case I am pointing to a Framed CMS, so it has to know what to load into the main frame (pun intended).

<h1>
    Page Heading
    <a href="/admin/main/?load=pages/page.cfm?id=#request.pageid#" title="Edit this page"
        onclick="return loadAdmin(this);"
        target="#cgi.server_name#wvAdmin"            
    ><img src="/media/admin/icon-edit.png" alt="Edit this page"/></a>
</h1>

This to make sure the tab is unique, in case I have several CMSs in my browser (support multiple clients):

target="#cgi.server_name#wvAdmin"

Finally, on the frame loader in the CMS, add this...

<script type="text/javascript">
    window.name = '#cgi.server_name#wvAdmin';
</script>

... to ensure that if the user opened the CMS on their own, we can still work with it.

Jules
  • 1,941
  • 15
  • 18
0

The simple answer is: you can't. Browsers doesn't expose that kind of API to javascript.

WTK
  • 16,583
  • 6
  • 35
  • 45
  • browser also don't allow for cross-domain scripting/accessing. Nontheless there is a firefox `about:config` setting where you can overwrite that restriction. Chrome does have a commandline argument for that, so it's not that absurd to expect to have a config for that also. – jAndy Nov 18 '11 at 10:24
  • Chrome does have a commandline argument for what? For cross-domain scripting, maybe so. But you've checked firefox config already - without results concerning tab focus. I've also done some research in that area - without positive results. When I answered with simple "you can't" I was refering directly to focusing tab/window. That was your question. That doesn't change the fact, that there are other options that are not enabled/allowed by default, available for manual change using about:config. – WTK Nov 18 '11 at 10:33
-2

Opening new browser windows/tabs is problematic. Have you considered jqueryui dialogs instead of opening a new window?

Ender
  • 213
  • 1
  • 7