0

I'm trying to focus an existing tab when the content reloads. The usual window methods don't seem to work.

Here's whats happening: On page_1 I have a link like...

<a href="page_2.html" target="page_2">Go to my other page</a>

If the tab doesn't exist, when the link is clicked it opens a new tab and takes focus. (Perfect)

If you then go back to page_1 and click the link again, it reloads the content in the existing tab (perfect) but doesn't focus (crap). I've tried the usual window.focus, $(window).focus methods on load with page_2 without luck.

Any recommendations?

4 Answers4

1

It is impossible.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
1

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
  • FYI, it's not allowed to post the same answer with same content again and again. You have already posted the [same answer](http://stackoverflow.com/questions/8135188/focus-tab-or-window/15247882#15247882) earlier. – Paresh Mayani Mar 06 '13 at 14:04
0

There is a workaround to this. Use javascript to open a window in a new tab, store a reference to that tab, and when you want to focus it; close it first and then re-open it.

if (window.existingWindow != null) 
  try { window.existingWindow.close(); } catch (e) { };
window.existingWindow = window.open("/your/url", "yourTabName");

We use a similar approach to opening the preview pane of the current page you're working on in our service called Handcraft where the above works as expected (we wanted the new window to always focus).

Martin Kool
  • 4,188
  • 4
  • 25
  • 36
-1

Without using a framework you can put a script block at the bottom of your page that will run once the page loads. Because it is after your HTML you can be assured that the HTML is refers to is actually available.

The script can set the focus to the element you want.

Diodeus - James MacFarlane
  • 112,730
  • 33
  • 157
  • 176