Open a new tab with javascript but stay on current tab using javascript
Asked Answered
A

4

29

Is it possible to open a new tab in browser using the window.open("http://www.google.com"); function, but open it in the background and remain on the current page? When i Click in hyperlink the page will same but link will open in new tab...

I try this solution but it work only in Firefox link but I want to do in all browsers.

Asuncion answered 29/11, 2013 at 4:43 Comment(3)
Doesn't look like a duplication to me. This question is specific for the keeping the current tab [window] in the foreground, not tab vs window.Otey
https://mcmap.net/q/40446/-open-a-url-in-a-new-tab-and-not-a-new-window is one of possible tricks to solve this issueSelfwill
Absolutely not a duplicate. Such BS. Who "reviewed" it and decided to keep it closed?Commuter
V
8

As confirmed in both: source1 source2

there isn't a function that works throughout all browsers. There are options for popups, but this isn't a good idea as many use popup blockers.

To reiterate the first source, it's a browser setting for each user to decide to open a new tab in the background, or not. And because users decide this in their browser settings you will get inconsistent experiences.

Virgulate answered 29/11, 2013 at 4:59 Comment(2)
I searched for ~60 minutes on the topic of opening a non-focused new tab and this is the only true valid answer on this topic as of November 2017!Tray
You say for each user to decide to open a new tab in the background but if I write JavaScript for my use then I still cannot choose to keep the current tab active when I open a new tab (window). So users cannot make the decision.Mazzard
E
3

Try following may be helpful

<button id="open">open</button>

document.getElementById('open').onclick = function() {
    window.open('http://google.com');   
};

Note: 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 in about:config in Firefox is:

It is only possible if you will be generate the Click event with Already Pressed Control Key Dynamically.

e.g. Ctrl + Click will always open new tab and stay you on current tab.

browser.tabs.loadDivertedInBackground=true

Emmerich answered 29/11, 2013 at 5:1 Comment(0)
S
0

Use onMouseUp + function onOpenInNewTab

...
return (
  <button
    key={item.title}
    onMouseUp={item.onClick}
  >
    {item.title}
  </button>
);
const openInNewTab = (url: string): void => {
    const newWindow = window.open(url, '_blank', 'noopener,noreferrer');

    if (newWindow) {
        newWindow.opener = null;
    }
}

where item.onClick is a function with openInNewTab(link) call inside for each menu item

Saccharase answered 17/5, 2024 at 6:19 Comment(0)
N
-11

try this code

 window.open(url,'_blank');
Narcosis answered 29/11, 2013 at 5:20 Comment(1)
this doesnt solve the question ... it opens but loses focusCarl

© 2022 - 2025 — McMap. All rights reserved.