118

In chrome this opens in a new tab:

<button onclick="window.open('newpage.html', '_blank')" />

this opens in a new window (but I'd like this to open in a new tab as well:

<script language="javascript">
  window.open('newpage.html', '_blank');
</script>

Is this feasible?

Sheena
  • 15,590
  • 14
  • 75
  • 113
Andrew
  • 3,545
  • 4
  • 31
  • 37
  • 2
    Are you sure about what you affirm ? Both fragments have the same result. – Denys Séguret Apr 04 '13 at 18:28
  • 1
    Whether a window opens in a tab or a new window is up to the local browser and how the user configures their browser - it is not something you can specify in javascript. – jfriend00 Apr 04 '13 at 18:29

8 Answers8

153

You can't directly control this, because it's an option controlled by Internet Explorer users.

Opening pages using Window.open with a different window name will open in a new browser window like a popup, OR open in a new tab, if the user configured the browser to do so.

EDIT:

A more detailed explanation:

1. In modern browsers, window.open will open in a new tab rather than a popup.

2. You can force a browser to use a new window (‘popup’) by specifying options in the 3rd parameter

3. If the window.open call was not part of a user-initiated event, it’ll open in a new window.

4. A “user initiated event” does not have to the same function call – but it must originate in the function invoked by a user click

5. If a user initiated event delegates or defers a function call (in an event listener or delegate not bound to the click event, or by using setTimeout for example), it loses it’s status as “user initiated”

6. Some popup blockers will allow windows opened from user initiated events, but not those opened otherwise.

7. If any popup is blocked, those normally allowed by a blocker (via user initiated events) will sometimes also be blocked. Some examples…

Forcing a window to open in a new browser instance, instead of a new tab:

window.open('page.php', '', 'width=1000');

The following would qualify as a user-initiated event, even though it calls another function:

function o(){
  window.open('page.php');
}
$('button').addEvent('click', o);

The following would not qualify as a user-initiated event, since the setTimeout defers it:

function g(){
  setTimeout(o, 1);
}
function o(){
  window.open('page.php');
}
$('button').addEvent('click', g);
Prakash Chennupati
  • 3,066
  • 3
  • 27
  • 38
  • 2
    I just want to add that by testing in Chrome and FF I found out that using window.open in what you define as a “user initiated event” keeps the browser default action. By that I mean that if in Chrome you hold shift on chrome and click a new browser window will appear, if you hold ctrl (or press the middle mouse button) a new tab will open in background. Which is very nice, you can make some things without needing to place anchor tags all over. – Hoffmann Aug 12 '13 at 19:43
  • 3
    *"If the window.open call was not part of a user-initiated event, it’ll open in a new window."* No, it almost certainly won't get opened at all. – T.J. Crowder May 30 '14 at 07:40
  • 3
    As of 9/29/2014, latest Firefox ESR and IE 11 open _blank or window.open links in a new window when the js calls use _blank as the window name. Current Chrome isn't that simple. I tested opening a new window passing top, left, width, height, toolbar, location, directories, status, menubar, scrollbars, and resizable, and omitting each one of those in turn. Chrome opened them all in a new tab except when status, menubar, or scrollbars was omitted. This seems so arbitrary it's hard to believe, but empirically that's what happened. Example here: http://jsbin.com/mobiyeqojaha/1 – enigment Sep 29 '14 at 14:55
35

It is sometimes useful to force the use of a tab, if the user likes that. As Prakash stated above, this is sometimes dictated by the use of a non-user-initiated event, but there are ways around that.

For example:

$("#theButton").button().click( function(event) {
   $.post( url, data )
   .always( function( response ) {
      window.open( newurl + response, '_blank' );
   } );
} );

will always open "newurl" in a new browser window since the "always" function is not considered user-initiated. However, if we do this:

$("#theButton").button().click( function(event) {
   var newtab = window.open( '', '_blank' );
   $.post( url, data )
   .always( function( response ) {
      newtab.location = newurl + response;
   } );
} );

we open the new browser window or create the new tab, as determined by the user preference in the button click which IS user-initiated. Then we just set the location to the desired URL after returning from the AJAX post. Voila, we force the use of a tab if the user likes that.

Frank A Tinker
  • 359
  • 3
  • 3
  • I noticed that if you have a break point set in chrome on the window.open then the browser opens a new window. Letting the code not break seems to result in a new tab being opened instead. – skinnybrit51 Jul 15 '14 at 17:06
  • It is an OK workaround unless your window.open and the result of the function is something that takes 5/6/7 seconds or so. Consider if the function is posting data to a server that is generating a PDF which takes 10 seconds. The window.open to a blank tab is immediate and the user stares at the blank tab for 10 seconds until magically it gets populated. Still on iOS it is about the only solution. – Kevin Brown Aug 21 '18 at 23:08
14

if you use window.open(url, '_blank') , it will be blocked(popup blocker) on Chrome,Firefox etc

try this,

$('#myButton').click(function () {
    var redirectWindow = window.open('http://google.com', '_blank');
    redirectWindow.location;
});

working js fiddle for this http://jsfiddle.net/safeeronline/70kdacL4/2/

working js fiddle for ajax window open http://jsfiddle.net/safeeronline/70kdacL4/1/

Mohammed Safeer
  • 20,751
  • 8
  • 75
  • 78
11

At the moment (Chrome 39) I use this code to open a new tab:

window.open('http://www.stackoverflow.com', '_blank', 'toolbar=yes, location=yes, status=yes, menubar=yes, scrollbars=yes');

Of course this may change in future versions of Chrome.

It is a bad idea to use this if you can't control the browser your users are using. It may not work in future versions or with different settings.

Nico Wiedemann
  • 174
  • 2
  • 10
  • As per jfriend00's comment, the way the user configures the browser determines how new windows are opened, not code. – Lee Taylor Oct 15 '14 at 22:29
  • I agree with you, that it is a bad idea to rely on such a solution if you can't control your environment. But there are some situations where this is possible (for example I use it in an application which ships with its own portable Chrome). To make this clear I edited my anser. – Nico Wiedemann Oct 22 '14 at 09:18
  • 2
    For those wondering, running this code snippet doesn't work because of this error: `VM646 js:12 Blocked opening 'http://www.stackoverflow.com/' in a new window because the request was made in a sandboxed frame whose 'allow-popups' permission is not set.` – Benjamin Crouzier Nov 03 '16 at 17:06
6

This will open the link in a new tab in Chrome and Firefox, and possibly more browsers I haven't tested:

var popup  = $window.open("about:blank", "_blank"); // the about:blank is to please Chrome, and _blank to please Firefox
popup.location = 'newpage.html';

It basically opens a new empty tab, and then sets the location of that empty tab. Beware that it is a sort of a hack, since browser tab/window behavior is really the domain, responsibility and choice of the Browser and the User.

The second line can be called in a callback (after you've done some AJAX request for example), but then the browser would not recognize it as a user-initiated click-event, and may block the popup.

Magne
  • 16,401
  • 10
  • 68
  • 88
  • 1
    `$window` is just AngularJS's way of using `window`. You can simply use `window` instead. More info: https://docs.angularjs.org/api/ng/service/$window – Magne Nov 12 '14 at 11:19
3

Clear mini-solution $('<form action="http://samedomainurl.com/" target="_blank"></form>').submit()

Serhii Matrunchyk
  • 9,083
  • 6
  • 34
  • 47
0

You can use this code to open in new tab..

function openWindow( url )
{
  window.open(url, '_blank');
  window.focus();
}

I got it from stackoverflow..

Community
  • 1
  • 1
Dilip Rajkumar
  • 7,006
  • 6
  • 60
  • 76
  • 1
    This works, but only if you are in a user click handler. Otherwise, it opens a new window (but Firefox at least will block you until the user allows it). – djsmith Aug 14 '13 at 19:32
  • @djsmith, I am using this.. and facing issue only in IE7. If you have any other solution please suggest.. Thank you.. – Dilip Rajkumar Aug 16 '13 at 05:23
-2

Best way i use:

1- add link to your html:

<a id="linkDynamic" target="_blank" href="#"></a>

2- add JS function:

function OpenNewTab(href)
{
    document.getElementById('linkDynamic').href = href;
    document.getElementById('linkDynamic').click();
}

3- just call OpenNewTab function with the link you want

Mohammad Shraim
  • 1,173
  • 12
  • 21