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.
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.
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.
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.
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
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(); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With