18

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

4 Answers 4

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
5
  • 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 2, 2011 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 2, 2011 at 11:26
  • 1
    BUT this only works on YOUR machine, as it is a browser preference.
    – Anze Jarni
    Jun 2, 2011 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 2, 2011 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 2, 2011 at 11:40
6

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')" />
2
  • 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. =) Apr 10, 2015 at 2: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 stackoverflow.com/questions/61163531/…
    – Nikkorian
    Feb 20, 2023 at 14:53
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.

5
  • 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 2, 2011 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 2, 2011 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, 2019 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, 2019 at 17:49
  • The issue I posted here is about what is said here.
    – Sam Hobbs
    Sep 13, 2019 at 19:01
-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> 
1
  • 3
    The OP specifically said he wanted to stay on the current tab.
    – Adam B
    Jun 21, 2015 at 1:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.