17

Is it possible to open a new tab in Firefox (in background) using window.open("http://www.google.com") function, and remain the current tab?

Thanks for any help

Anas Abu Farraj
  • 1,540
  • 4
  • 23
  • 31
Daniel H
  • 2,865
  • 8
  • 34
  • 32

4 Answers4

13

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
dogbane
  • 266,786
  • 75
  • 396
  • 414
  • Hi dogbane, where is this setting? This is for an in-office website which won't be used outside so changing the browser settings could be a solution for us – Daniel H Jun 02 '11 at 11:25
  • Type `about:config` in your firefox address bar and search for: `browser.tabs.loadDivertedInBackground`. If true, it will load the new tab in the background, leaving focus on the current tab – dogbane Jun 02 '11 at 11:26
  • 1
    BUT this only works on YOUR machine, as it is a browser preference. – Anze Jarni Jun 02 '11 at 11:32
  • 1
    @Anze Jarni: You are, of course, correct. However, @dogbane mentions that in the answer, and @Daniel H also mentions _This is for an in-office website which won't be used outside so changing the browser settings could be a solution for us_ in the comments above. – Town Jun 02 '11 at 11:33
  • 1
    In that case, if you want to install firefox on all machines with fresh default settings, just edit prefs.js (which should be in your user profile directory) and copy it across all machines. – Anze Jarni Jun 02 '11 at 11:40
6

Whether or not to focus a new tab when it is opened is a browser setting and not something that you can control.

Opening links in a new tab at all (rather than a separate window) is a browser setting too, so you're facing an uphill battle with this one.

Basically, leave it up to the user to decide how they want to open links.

Town
  • 14,706
  • 3
  • 48
  • 72
  • Thanks for your answer Town. This webpage won't be used by the general public, only in-office so if I can change the browser settings on each of our computers I'd be happy with that. – Daniel H Jun 02 '11 at 11:26
  • @Daniel H: In that case, dogbane has the answer. Type `about:config` into your address bar and locate the setting. – Town Jun 02 '11 at 11:27
  • But if I am the user and I **want** to keep the default of switching to new tabs but then in JavaScript I want to **override** that then I cannot do that. I cannot make the decision. I want to open a list of sites without having to manually click back to the window that opens them. I know you cannot change the browsers but the justification provided here has flaws. – Sam Hobbs Sep 12 '19 at 17:40
  • @user34660 That sounds more like an issue that you have with the config options provided by the manufacturer of your preferred browser, rather than about overriding the settings of someone else's. – Town Sep 12 '19 at 17:49
  • The issue I posted here is about what is said here. – Sam Hobbs Sep 13 '19 at 19:01
5

Here's an idea:

<script>
function open_in_bg(c_url, n_url)
{
 window.open (n_url, "mywindow" );
 window.open (c_url+"#maintain_focus","_self");
}
</script>

<input type="button" onclick="open_in_bg('current_page_url', 'url_to_be_opened')" />
Anze Jarni
  • 1,141
  • 7
  • 7
  • 1
    Shame you got marked down. this is the closest I have seen. It doesnt work on first click but actually works on the 2nd click for some reason. I wonder if that is a clue. =) – Gary Carlyle Cook Apr 10 '15 at 02:42
  • @GaryCarlyleCook This is because a browser will only ever open one new window for a single user action. See the only answer and comments in https://stackoverflow.com/questions/61163531/js-how-to-open-new-tab-multiple-times-in-loop – Nikkorian Feb 20 '23 at 14:53
-6

I know you said JavaScript, OP, but, I feel the heart of the matter for you is just opening the html links in and of themselves in a new tab.

In which case, simple add ' target="_blank" ' inside your link tags:

<a href="example.com" target="_blank"> random stuff</a> 
Eleanor Zimmermann
  • 414
  • 1
  • 8
  • 26