skip to Main Content

I am using the snippet below to open a URL in new tab

window.open(urlToOpen, '_blank', 'noopener noreferrer');

This part of code when hit for the first time, opens the URL in new tab but also shifts the focus to the new tab as well but all the consecutive clicks in the same session result in opening the URL in new tab but focus remains on the existing tab.
Based on my understanding, this particular part of snippet would have opened URL always in new tab and focus should not have shifted to new tab.

Is there any reason why this might be happening?(I have tried this across different browsers)

2

Answers


  1. There is an old answer that suggests open the tab, close it and then open it again fixes the issue.

    https://stackoverflow.com/a/15247882/2087070

    The problem is that focus on open tab only works when you open a new tab, not when you link to an already open tab in the browser, so closing and reopening it fixes the issue, as the browser then sees this tab as fresh.

    Login or Signup to reply.
  2. <!DOCTYPE html>
    <html>
        <head>
        </head>
        <body>
            <a href="https://www.youtube.com/" target="blank">youtube</a>
        </body>
    </html>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search