I am diverting user to some url through window.location
but this url opens in the same tab in browser. I want it to be open in new tab. Can I do so with window.location? Is there another way to do this action?

- 22,654
- 47
- 125
- 190
-
3Duplicate: http://stackoverflow.com/questions/427479/programmatically-open-new-pages-on-tabs – Samich Sep 26 '11 at 11:05
-
1Is `window.location` a requirement? Or can other JS solutions be offered ? – Khez Sep 26 '11 at 11:05
-
@Khez: other JS can be offered. – Muhammad Imran Tariq Sep 26 '11 at 11:33
-
you can use the window.open() – Junaid khan Jun 07 '21 at 12:51
7 Answers
window.open('https://support.wwf.org.uk', '_blank');
The second parameter is what makes it open in a new window. Don't forget to read Jakob Nielsen's informative article :)

- 8,633
- 8
- 40
- 51
-
11but what if your browsers has blocked settings on popup? this will not wok. – pregmatch Aug 05 '17 at 11:01
-
@Alex meh...not really the "right" answer. Trying this in Firefox, where I prevent pop-up windows, this code fails. – TARKUS Dec 02 '19 at 16:32
You can even use
window.open('https://support.wwf.org.uk', "_blank") || window.location.replace('https://support.wwf.org.uk');
This will open it on the same tab if the pop-up is blocked.

- 777
- 6
- 13
-
I would avoid this solution because window.open in some cases could return null (but still open a new window as expected) while also triggering location.replace on the current tab resulting in two open tabs with the new url. Using a try/catch described in this answer may be a better solution https://stackoverflow.com/a/27725432/811533. – Troy Watt Jun 16 '22 at 14:17
I don't think there's a way to do this, unless you're writing a browser extension. You could try using window.open
and hoping that the user has their browser set to open new windows in new tabs.

- 10,916
- 6
- 42
- 49
This works for me on Chrome 53. Haven't tested anywhere else:
function navigate(href, newTab) {
var a = document.createElement('a');
a.href = href;
if (newTab) {
a.setAttribute('target', '_blank');
}
a.click();
}

- 12,607
- 11
- 69
- 123
with jQuery its even easier and works on Chrome as well
$('#your-button').on('click', function(){
$('<a href="https://www.some-page.com" target="blank"></a>')[0].click();
})

- 3,042
- 1
- 28
- 57
Rather going for pop up,I personally liked this solution, mentioned on this Question thread JavaScript: location.href to open in new window/tab?
$(document).on('click','span.external-link',function(){
var t = $(this),
URL = t.attr('data-href');
$('<a href="'+ URL +'" target="_blank">External Link</a>')[0].click();
});
Working example.

- 320
- 2
- 8
We have to dynamically set the attribute target="_blank" and it will open it in new tab.
document.getElementsByTagName("a")[0].setAttribute('target', '_blank')
document.getElementsByTagName("a")[0].click()
If you want to open in new window, get the href link and use window.open
var link = document.getElementsByTagName("a")[0].getAttribute("href");
window.open(url, "","height=500,width=500");
Don't provide the second parameter as _blank in the above.

- 217
- 2
- 11