Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Window.open with 'noopener' opens a new window instead of a new tab

I was using window.open('') with '_blank' as second parameter to open my link in new tab For eg. window.open('http://google.com', '_blank')

But, recently I added the third parameter 'noopener' so that window.opener becomes null in the new tab and the new tab does not have access to the parent tab/window. i.e. window.opener is null

window.open('http://google.com', '_blank', 'noopener')

So the above code solved the security problem, but instead of opening a new tab, a new window started opening which is not what I expected. My browser settings were same and no changes were made to it.

Is there anything I can do to make this code open new tab instead of new window ? I do not want to remove noopener as third parameter

like image 940
Chirag Swadia Avatar asked Mar 14 '18 11:03

Chirag Swadia


2 Answers

Another approach that will solve this in one line is to access the opener property directly and set it to null to make use of the fact that window.open() returns a Window object. This will work across all browsers to open a new tab with a null window.opener.

window.open(url, '_blank').opener = null;
like image 151
keimjohn Avatar answered Sep 21 '22 18:09

keimjohn


Honestly i think your code is fine but you can try a different implementation:

var yourWindow = window.open();
yourWindow.opener = null;
yourWindow.location = "http://someurl.here";
yourWindow.target = "_blank";
like image 40
Ricardo Costa Avatar answered Sep 17 '22 18:09

Ricardo Costa