Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inconsistency in Focus Behavior when Opening a URL in a New Tab Using window.open()

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)

like image 382
sporty_1993 Avatar asked Dec 05 '25 13:12

sporty_1993


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

like image 52
David Bradshaw Avatar answered Dec 08 '25 06:12

David Bradshaw



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!