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