28

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.

Community
  • 1
  • 1
Panchotiya Vipul
  • 1,226
  • 5
  • 17
  • 42
  • 10
    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. – niry May 08 '18 at 02:07
  • stackoverflow.com/a/59509486/1916821 is one of possible tricks to solve this issue – Vaha Dec 28 '19 at 09:04
  • 2
    Absolutely not a duplicate. Such BS. Who "reviewed" it and decided to keep it closed? – Emperor Eto May 19 '22 at 20:48

3 Answers3

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.

Community
  • 1
  • 1
Pippin
  • 1,066
  • 7
  • 15
  • 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! – poitroae Nov 04 '17 at 20:17
  • 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. – Sam Hobbs Sep 12 '19 at 17:32
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

Ashwin Parmar
  • 3,025
  • 3
  • 26
  • 42
-9

try this code

 window.open(url,'_blank');
mano
  • 58
  • 3