329

How to open a URL in new tab instead of new window programatically?

Brett DeWoody
  • 59,771
  • 29
  • 135
  • 184
Midhuna
  • 4,523
  • 4
  • 21
  • 22

6 Answers6

523

Use window.open():

var win = window.open('http://stackoverflow.com/', '_blank');
if (win) {
    //Browser has allowed it to be opened
    win.focus();
} else {
    //Browser has blocked it
    alert('Please allow popups for this website');
}

Depending on the browsers implementation this will work

There is nothing you can do to make it open in a window rather than a tab.

milosmns
  • 3,595
  • 4
  • 36
  • 48
FabianCook
  • 20,269
  • 16
  • 67
  • 115
  • 3
    What does `_blank` do in this call? – Kellen Stuart Feb 13 '17 at 20:22
  • 3
    It's a special name of a browser window that will create a new window every time, alternatively you could use a specific name like "login_screen", if the window is already loaded it will reload the content with the URL you have provided – FabianCook Feb 16 '17 at 10:49
  • 5
    `window.open()` would cause the popup warning which is an unwanted user experience. Instead introduce a hidden `` somewhere and perform a click using `.click()` For ex: `document.getElementById('click_me').click()` – nehem Feb 26 '20 at 03:53
217

This is as simple as this.

window.open('_link is here_', 'name'); 

Function description:

name is a name of the window. Following names are supported:

  • _blank - URL is loaded into a new tab. This is default.
  • _parent - URL is loaded into the parent frame
  • _self - URL replaces the current page
  • _top - URL replaces any framesets that may be loaded
The Codesee
  • 3,714
  • 5
  • 38
  • 78
JaiSat
  • 2,356
  • 1
  • 14
  • 15
  • 6
    For security, one could add the "noopener" and "noreferrer" window features: window.open("url", "_blank", "noopener,noreferrer"); – kas Jun 03 '20 at 05:00
74

if you mean to opening all links on new tab, try to use this jquery

$(document).on('click', 'a', function(e){ 
    e.preventDefault(); 
    var url = $(this).attr('href'); 
    window.open(url, '_blank');
});
Lafif Astahdziq
  • 3,788
  • 29
  • 38
  • @Vicky is your `a` element appended by other event? May you need to change `.click` with `.on('click'` – Lafif Astahdziq Oct 20 '15 at 01:12
  • i am trying to implement same logic using your answer which works but when i try to right click and do open link in new tab it doesnt. any idea how can i fix that? – mike Jan 02 '18 at 20:23
46
 var url = "http://www.example.com";
 window.open(url, '_blank');
WSBT
  • 33,033
  • 18
  • 128
  • 133
codebreaker
  • 1,465
  • 1
  • 12
  • 18
28

You can easily create a new tab; do like the following:

function newTab() {
     var form = document.createElement("form");
     form.method = "GET";
     form.action = "http://www.example.com";
     form.target = "_blank";
     document.body.appendChild(form);
     form.submit();
}
Shane Reustle
  • 8,633
  • 8
  • 40
  • 51
Koffy
  • 768
  • 8
  • 19
  • Hi dude.. when I open like this im getting like firefox prevented this site from opening popup.... – sarathkumar Sep 04 '14 at 10:34
  • 3
    Amend your implementation to be user-driven (i.e. Click a link) and not self-executing (i.e. On page load) ... then praise the righteous popup blocker – Koffy Nov 02 '14 at 15:20
15

I know your question does not specify if you are trying to open all a tags in a new window or only the external links.

But in case you only want external links to open in a new tab you can do this:

$( 'a[href^="http://"]' ).attr( 'target','_blank' )
$( 'a[href^="https://"]' ).attr( 'target','_blank' )
Daniel Falabella
  • 578
  • 4
  • 13