Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chrome, Javascript, window.open in new tab

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?

like image 503
Andrew Avatar asked Oct 04 '22 23:10

Andrew


People also ask

How do I get links to open in a new tab automatically in Chrome?

Use Mouse or Trackpad Only If you use a mouse, simply utilizing the middle mouse button to click on a link will immediately open it in a new browser tab! Holding down the Shift key while middle-clicking also helps you switch to the tab automatically.

How do I keep a link window open in a new tab?

Yes, either change the browser preferences or use whatever key and click combination is required for that browser to open the link in a new tab but keep focus on the current on. You can't do it with script. Leave it up to the user.

How can you open a link in a new tab browser window in HTML?

You can make a HTML link open in a new tab by adding the target=”_blank” attribute. You should insert this after the link address.


2 Answers

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);
like image 70
Prakash Chennupati Avatar answered Nov 04 '22 09:11

Prakash Chennupati


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.

like image 45
Frank A Tinker Avatar answered Nov 04 '22 11:11

Frank A Tinker