It seems that you're using an outdated browser. Some things may not work as they should (or don't work at all).
We suggest you upgrade newer and better browser like: Chrome, Firefox, Internet Explorer or Opera

×
The goal is to open new tab with GOG site but I would like to stay focused on the tab with the button - just like the way "Open Link in New Tab" option from right-click menu works.

At this moment clicking the button creates new tab and switches the focus to it - how to prevent that?

My code is (I've replaced < > with [ ]):

[button onclick="var tab=window.open('https://www.gog.com','_blank'); tab.blur();"]GOG[/button]

The JavaScript option I've googled about (.blur) does not seem to work on my Firefox browser.
Post edited September 23, 2021 by Lexor
No posts in this topic were marked as the solution yet. If you can help, add your reply
avatar
Lexor: The goal is to open new tab with GOG site but I would like to stay focused on the tab with the button - just like the way "Open Link in New Tab" option from right-click menu works.
I'm not aware of any way to do this with JavaScript. (That doesn't mean it doesn't exist, but it does mean it's unlikely to exist; browsers have very deliberately not given web pages a way to affect other tabs, since that ability could cause great havoc when combined with phishing.)

Just in case, are you aware that middle-click (press the scroll-wheel rather than scrolling it) will also open a link in a new tab without switching focus, and is (for me at least) exactly as easy as regular single-click? I'd say 90% of all links I click are opened in new tabs this way.
avatar
gogtrial34987: I'm not aware of any way to do this with JavaScript.
But the .blur() still exists. Maybe I am doing something wrong? I'm not an expert in javascript.
The thing is, that JavaScript thinks it's opening a new window (y'know, a "popup"). And that you could blur (leading to the even more hated "pop-under").
Nowadays browsers redirect "new windows" to "new tabs" (there'a setting for that somewhere which controls that) - but the rest of the behaviour deliberately doesn't carry over to tabs.
OK, so maybe there is some other way like replacing "click" with simulation of "ctrl+click"?
I've seen some code for Chrome, maybe there is something similar for Firefox?
According to this page: https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent this code should work:

[a id="test" href="page.htm"]link[/url]
[button onclick="document.getElementById('test').dispatchEvent(new MouseEvent('click',{ctrlKey: true}));"]button[/button]

... but it does not. Any ideas why?
avatar
Lexor: According to this page: https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent this code should work:

[a id="test" href="page.htm"]link[/url]
[button onclick="document.getElementById('test').dispatchEvent(new MouseEvent('click',{ctrlKey: true}));"]button[/button]

... but it does not. Any ideas why?
I don't know why, but, many browsers and mouses have the built in function of middle-click = opens a new tab without focusing on them.
Maybe searching for a command that forces the click to go to middle-mouse-button-click works?
avatar
.Keys: Maybe searching for a command that forces the click to go to middle-mouse-button-click works?
https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/MouseEvent
I've replaced "ctrlKey: true" with "button: 1" and also with "buttons: 4" - these options do not work either.

Maybe there is something wrong with the syntax of my JavaScript code?
Post edited September 24, 2021 by Lexor
I've done a few examples with https://developer.mozilla.org/en-US/docs/Web/API/Window/focus

It seems that I can switch focus on demand between tabs but only if I know their handlers.

This example works:

[button onclick="
var tab1=window.open('page.htm','_blank');
var tab2=window.open('page.htm','_blank');
setTimeout(function(){tab1.focus();},1000);
"]button[/button]

It creates two additional tabs and then switch focus to the first one after 1 second.

What command I should use to get the "handle" of the parent / original window (with button) to switch focus back to it?
avatar
gogtrial34987: ...
avatar
.Keys: ...
I've found the first solution! :D

[button onclick="var tab1=window.open('','_self'); var tab2=window.open('page.htm','_blank'); tab1.focus();"]button[/button]

It opens "empty" link in the current tab to get its handler, then it opens another, desired tab, and then it switches focus to the original, parent tab with the button. :D

Question: Are there any other solutions? Can I use something different to directly get the handler of the current tab?