Stop bringing new pages to foreground



It seems that there is some code that calls some kind of focus() after a discourse page successfully loads — witnessed it in meta, in try and in MB.

This code has no effect in Firefox, Chromium, Windows Mobile Edge and probably many other modern browsers, so this code may be simply forgotten behind.

You may actually only be able to witness that focus() thing if you use Opera presto and open link in background tab (my case and it’s bothering that tabs opened in the background, suddenly steal the focus and are brought to the foreground).

The fact that this code does not work in most common browsers makes it quite like dead code.
Could it be removed, sooner or later?

With kind regards.

(Jeff Atwood) #2

I’m not aware of any such code or behavior. What are your exact repro steps?


Hello @codinghorror,
I do like this:

Note that I have used Opera presto (v12). So, of course, you may not loose too much time on this.
It can also come from an included code (plugin, library, etc.).

  1. I go to
  2. I right‐click→Open in Background Tab on any topic

Then instead of new tab always kept in background, I see the new background tab loading in background, good, but when it’s fully loaded, it’s brought to foreground.
So when I open several topics in background, they will jump in front, in turn, as they are loaded.

I can see several .trigger("focus") or .focus(); in JS files but it’s very hard to guess which one…

(Jeff Atwood) #4

This is a wildly obsolete browser:

Last version relased in March 2015. We only support currently shipping, stable browsers.


That’s completely legitimate, I was only pointing out that maybe newer browsers just prevent background tab from being brought foreground and that there might be some code lingering somewhere, trying to do that.
This code could be considered as dead code if its purpose is defeated by all modern browsers.
It was just a cleanup hint. :slight_smile:
But there is so much JavaScript that it might be very difficult to pin‐point.

(Régis Hanol) #6

We don’t use focus() to bring the window in the foreground, that would be rude.
We only use focus() to put the cursor on the first field that requires input (often used in modals or in the composer).

If your browser is bringing the background tab in the foreground, then I’m sorry for you :wink:


My browser does not bring a tab to the foreground when one of its inputs gets the focus.
I have made sure of that at by typnig in the address bar:

javascript:void(setTimeout(function() { document.querySelector("a").focus(); }, 5000))

I have also tried on a page with a textarea:

javascript:void(setTimeout(function() { document.querySelector("textarea").focus(); }, 5000))

The a or textarea does get the focus but the tab is not brought to the foreground when it’s in the background.

(cpradio) #8

I did a search on the repo and for the life of me was unable to pinpoint any specific focus call that would explain a background tab moving to the foreground

Search · .focus · GitHub