Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open a new tab in the background?

Using javascript, I want to open a new page in a different 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 the new tab for a moment before switching back to the current tab. I want to avoid this.

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

like image 572
st-boost Avatar asked May 30 '12 08:05

st-boost


People also ask

What is open new tabs in background mean?

Question: Q: Open new tabs in background Answer: A: You have to tap on that tab to activate it and see that web page. That tab will open but you will still be on the page that you are currently viewing. You can open a tab in the background and keep browsing the page that you are already on.

How do I keep tabs active in the background?

To make a background tab active in Google Chrome automatically, you need to do the following. Press and hold Ctrl + Shift keys together on the keyboard, and only then click the link that you want to switch to immediately. It will be opened in a new foreground tab. This trick should work in all Chromium-based browsers.

What is a background tab?

The Background tab in the Style Editor allows you to modify the background color and image of the entire package, individual pages, columns on a page, or individual blocks. To use the Background tab: In the Design pane Styles tab, select the style you want to modify.


2 Answers

UPDATE: By version 41 of Google Chrome, initMouseEvent seemed to have a changed behavior, and so this answer no longer works. Thanks to @Daniel Andersson for his comment.

this can be done by simulating ctrl + click (or any other key/event combinations that open a background tab) on a dynamically generated a element 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

like image 181
Amro Avatar answered Nov 10 '22 09:11

Amro


THX for this question! Works good for me on 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(); } 
like image 34
user2837849 Avatar answered Nov 10 '22 09:11

user2837849