Using javascript, I want to open a new page in a different tab, but remain focused on the current tab. I know I can do it like this:
open('http://example.com/');
focus();
However, when I do this in chrome, it flashes the new tab for a moment before switching back to the current tab. I want to avoid this.
The application is a personal bookmarklet, so it only has to work in the latest Chrome.
I did exactly what you're looking for in a very simple way. It is perfectly smooth in Google Chrome and Opera, and almost perfect in Firefox and Safari. Not tested in IE.
Fiddle : http://jsfiddle.net/tFCnA/show/
Explanations:
Let's say there is windows A1 and B1 and websites A2 and B2.
Instead of opening B2 in B1 and then return to A1, I open B2 in A1 and re-open A2 in B1.
(Another thing that makes it work is that I don't make the user re-download A2, see line 4)
The only thing you may doesn't like is that the new tab opens before the main page.
Try this. It works fine with chrome, it also worked from chrome extension. All of the above did not work for me.
UPDATE: By version 41 of Google Chrome,
initMouseEvent
seemed to have a changed behavior.this can be done by simulating
ctrl
+click
(or any other key/event combinations that open a background tab) on a dynamically generateda
element with itshref
attribute set to the desiredurl
In action: fiddle
tested only on chrome
As far as I remember, this is controlled by browser settings. In other words: user can chose whether they would like to open new tab in the background or foreground. Also they can chose whether new popup should open in new tab or just... popup.
For example in firefox preferences:
Notice the last option.
Here is a complete example for navigating valid URL on a new tab with focused.
HTML:
CSS:
JAVASCRIPT:
I have also created a bin with the solution on http://codebins.com/codes/home/4ldqpbw
THX for this question! Works good for me on all popular browsers: