0

I have a web page with a link that opens another page in a new tab. In the newly opened page, I have a link that, when clicked, should bring focus to the original tab.

The new tab is opened via the target attribute:

<a href="/new-page/" target="_BLANK">new tab</a>

I thought this should work:

<a href="#" class="back-link">switch tabs</a>

$('.back-link').click(function() { 
    if(window.opener) {
      console.log('switching tabs');
      window.opener.focus();
    }
 });

The console.log fires, but focus stays on the current tab. What am I doing wrong?

j08691
  • 204,283
  • 31
  • 260
  • 272
Daveh0
  • 952
  • 9
  • 33

1 Answers1

1

Could you try adding the following event listener to your new tab button instead of target="_blank" :

$('.new-tab-button').click(function(e) { 
    e.preventDefault();
    window.open('/new-page/');
 });

This way you actually open a new window instead of new tab, according to the stackoverflow thread linked below, for security reasons, it seems to only be possible to focus to (and maybe from) windows/pop-ups. I'm not sure if what you're trying to do is possible but if so this could be it.

How to change browser focus from one tab to another

jasper
  • 937
  • 1
  • 9
  • 22