How to Open New Tab in JavaScript Without Switching to the New Tab

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 URL in a new tab (and not a new window)

Nothing an author can do can choose to open in a new tab instead of a new window; it is a user preference. (Note that the default user preference in most browsers is for new tabs, so a trivial test on a browser where that preference hasn't been changed will not demonstrate this.)

CSS3 proposed target-new, but the specification was abandoned.

The reverse is not true; by specifying certain window features for the window in the third argument of window.open(), you can trigger a new window when the preference is for tabs.

how to open window in new tab with javascript without changing browser settings

function open_in_new_tab(url )
{
var win=window.open(url, '_blank');
win.focus();
}

Open a URL in a new tab (and not a new window) using JavaScript

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

  1. If you are using the download link, then unpack the archive
  2. Navigate from chrome menu to extensions
  3. Enable the developer mode in the top right corner
  4. Click on Load Unpacked button to select the extension folder
  5. The extension will run automatically

JavaScript open in a new window, not tab

Specify window "features" to the open call:

window.open(url, windowName, "height=200,width=200");

When you specify a width/height, it will open it in a new window instead of a tab.

See https://developer.mozilla.org/en-US/docs/Web/API/Window.open#Position_and_size_features for all the possible features.



Related Topics



Leave a reply



Submit