Open a new tab in the background?
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
Open new tab in background leaving focus on current tab - Chrome
As you want this for personal usage, and you do not mind using an extension, then you can write your own one.
Writing a chrome extension to achieve your required behavior is actually super easy, All you need to know is how to open/close tabs from the extension.
The following page describes the whole API
Here is an example :
1 - Create a manifest.json
file, and ask for the tabs
permission
{
"name": "blabla",
"version": "0.1",
"permissions": ["tabs"],
"background": {
"persistent": false,
"scripts": ["background.js"]
},
"browser_action": {
"default_title": "Open a background tab every x time"
},
"manifest_version": 2
}
2 - Create background.js
script in the same folder
const INTERVAL = 5000;
setTimeout(function(){
chrome.tabs.create({url: "https://www.stackoverflow.com", active: false }, tab =>{
setTimeout(function(){
chrome.tabs.remove(tab.id);
},INTERVAL);
});
},INTERVAL);
You can download it from here too
Note: Please note the
active
parameter here , it defines whether the tab should become the active tab in the window. Does not affect
whether the window is focused
3 - Use the extension in chrome
- If you are using the download link, then unpack the archive
- Navigate from chrome menu to extensions
- Enable the developer mode in the top right corner
- Click on
Load Unpacked
button to select the extension folder - The extension will run automatically
How to open new tab in JavaScript without switching to the new tab?
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>
open a new browser tab in background programmatically
Back when popup ads were a thing, this was called a "popunder" window. Popunders used to do something like this:
var popupWindow = window.open(...);
popupWindow.blur();
window.focus();
Popup blocking kind of messed around with what does and doesn't work, though- your mileage may vary.
Google Chrome - how to open new window, in background
Try with that
var url = "yourURL.html";
window.open(url, "s", "width= 640, height= 480, left=0, top=0,
resizable=yes, toolbar=no, location=no, directories=no, status=no,
menubar=no, scrollbars=yes, resizable=no, copyhistory=no").blur();
window.focus();
Using blur/focus on window object you can manage which one appears active from time to time.
Open a new tab with javascript but stay on current tab
You can't open tabs in the background using javascript because this is set in the user's preferences in about:config
, which you have no control over. The setting is:
browser.tabs.loadDivertedInBackground=true
How can i open the new tab with chrome extension?
The "chrome_url_overrides newtab" value in your manifest is overriding the default new tab. Remove that from your manifest. If you want to show a page with instructions once before using the extension use the "chrome.runtime.onInstalled" event listener to open a page on installation. Or you could easily handle this with your own solution as well.
Related Topics
Jquery's Jquery-1.10.2.Min.Map Is Triggering a 404 (Not Found)
Node.Js on Multi-Core MAChines
How to Replace Innerhtml of a Div Using Jquery
Is Safari on iOS 6 Caching $.Ajax Results
What Do These JavaScript Bitwise Operators Do
Angularjs Uncaught Error: [$Injector:Modulerr] When Migrating to V1.3
How to Set Focus on an Input Field After Rendering
Parse Datetime String in JavaScript
Calling an Asynchronous Function Within a for Loop in JavaScript
Difference Between Microtask and MACrotask Within an Event Loop Context
How to Set the Value Property in Angularjs' Ng-Options
Parseint VS Unary Plus, When to Use Which
Why Does Firebase Lose Reference Outside the Once() Function
What Is Typescript and Why Would I Use It in Place of JavaScript