Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open new tab in JavaScript without switching to the new tab?

How can I open a new tab using javascript without switching to the new tab?
For example, when a user clicks on a link a new tab is to be opened, but the user should stay on the current tab.

like image 962
Miki Pavlov Avatar asked Oct 28 '11 01:10

Miki Pavlov


People also ask

How do I open a link in a new tab but stay on the same page JavaScript?

The JavaScript window. open() method allows you to open URL in the browser tab or window. You can use _self value in the second parameter of the window. open() method to open URL in the same tab and in the same window with JavaScript.

How do I make a link open in a new tab instead of a new window?

Method 1: Ctrl+Click Simply press and hold the Ctrl key (Cmd on a Mac) and then click the link in your browser. The link will open in a new tab in the background.


1 Answers

The web browser automatically focuses on the new tab, but you can call the focus back:

function openWindow( url ) {   window.open(url, '_blank');   window.focus(); }  <a href="http://www.example.com/" onclick="javascript:openWindow(this.href);return false;">Click Me</a> 
like image 149
Mike Avatar answered Sep 20 '22 15:09

Mike