Open a new tab in the background?

Using javascript, I want to open a new page in another tab, but remain focused on the current tab. I know I can do it like this:

open('http://example.com/'); focus(); 

However, when I do this in chrome, it flashes a new tab for a moment before switching to the current tab. I want to avoid this.

The application is a personal bookmarklet, so it should work only in the latest Chrome.

+59
javascript google-chrome
May 30 '12 at 8:28
source share
6 answers

UPDATE: as of version 41 of Google Chrome, initMouseEvent seems to have changed behavior.

this can be done by simulating ctrl + click (or any other combination of keys / events that open the background tab) for a dynamically created element a with its href attribute set to the desired url

In action: fiddle

 function openNewBackgroundTab(){ var a = document.createElement("a"); a.href = "http://www.google.com/"; var evt = document.createEvent("MouseEvents"); //the tenth parameter of initMouseEvent sets ctrl key evt.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0, true, false, false, false, 0, null); a.dispatchEvent(evt); } 

tested only on chrome.

+93
Jul 09 2018-12-12T00:
source share
— -

thanks for this question! Works well for me in all popular browsers:

 function openNewBackgroundTab(){ var a = document.createElement("a"); a.href = window.location.pathname; var evt = document.createEvent("MouseEvents"); //the tenth parameter of initMouseEvent sets ctrl key evt.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0, true, false, false, false, 0, null); a.dispatchEvent(evt); } var is_chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1; if(!is_chrome) { var url = window.location.pathname; var win = window.open(url, '_blank'); } else { openNewBackgroundTab(); } 
+7
Apr 04 '14 at 9:06
source share

As far as I remember, this is controlled by the browser settings. In other words: the user can choose whether they want to open a new tab in the background or foreground. They can also choose whether a new popup should open in a new tab or just ... pop up.

For example, in firefox settings:

Firefox setup example

Pay attention to the last parameter.

+1
Jul 03 2018-12-12T00:
source share

Try this. It works great with chrome, it also works on chromium expansion. All of the above did not help me.

 chrome.tabs.create({ url: "http://www.google.com/", selected: false }); 
0
Feb 01 '17 at 10:23
source share

I did exactly what you are looking for in a very simple way. It works great on Google Chrome and Opera and is almost perfect on Firefox and Safari. Not tested in IE.




 function newTab(url) { var tab=window.open(""); tab.document.write("<!DOCTYPE html><html>"+document.getElementsByTagName("html")[0].innerHTML+"</html>"); tab.document.close(); window.location.href=url; } 

Fiddle: http://jsfiddle.net/tFCnA/show/

Explanation:
Say there are windows A1 and B1 and sites A2 and B2.
Instead of opening B2 in B1 and then returning to A1, I open B2 in A1 and open A2 in B1 again.
(Another thing that it works is that I do not force the user to reload A2, see Line 4)




The only thing you may not like is to open a new tab in front of the main page.

-2
Jul 04 '12 at 23:14
source share

Here is a complete example for navigating to a valid URL in a new focused tab.

HTML:

 <div class="panel"> <p> Enter Url: <input type="text" id="txturl" name="txturl" size="30" class="weburl" /> &nbsp;&nbsp; <input type="button" id="btnopen" value="Open Url in New Tab" onclick="openURL();"/> </p> </div> 

CSS

 .panel{ font-size:14px; } .panel input{ border:1px solid #333; } 

JAVASCRIPT:

 function isValidURL(url) { var RegExp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/; if (RegExp.test(url)) { return true; } else { return false; } } function openURL() { var url = document.getElementById("txturl").value.trim(); if (isValidURL(url)) { var myWindow = window.open(url, '_blank'); myWindow.focus(); document.getElementById("txturl").value = ''; } else { alert("Please enter valid URL..!"); return false; } } 

I also created a bin with the solution at http://codebins.com/codes/home/4ldqpbw

-3
Jul 03 2018-12-12T00:
source share



All Articles