0

I'm using code below to simulate mouse click on the button:

someButton.dispatchEvent(new MouseEvent("click", { ctrlKey: true }));

But it keeps opening in new tab with the focus on new tab. I want it to behave just like the manual usage of ctrl+mouseClick when new tab opens in background.

There is no url under this button, it's submit button under the form with POST request.

Am I missing something here?

DarkBee
  • 16,592
  • 6
  • 46
  • 58
  • 1
    This would behaviour is probably intentional so that if a page is opened it is not hidden in the background – phuzi Jul 27 '22 at 13:02
  • There is a URL associated with the button if it posts a form: the form's `action` attribute. Browser makers are wise to the tricks spammers and the rest have come up with to create pop-unders and the like. You can't simulate everything a user can do; sometimes a user (or at least a process external to the browser) has to perform the action. – Heretic Monkey Jul 27 '22 at 13:05
  • Is 'someButton' truly a button or an `` element? – paddotk Jul 27 '22 at 13:06
  • @Heretic Monkey , the form has the action="/somepage/anotherpage" so if it's not possible to run the script to open that in new tab in background, is there any workaround for that? pop-ups are allowed in my broweser – theboldsammer Jul 27 '22 at 13:15
  • @paddotk yes, it is truly a button with submit type inside the form – theboldsammer Jul 27 '22 at 13:19
  • What might work is `event.preventDefault()` where `event` is the passed event object in the callback. The code would be something like `someButton.dispatchEvent(new MouseEvent("click", { ctrlKey: true }), myListener);` where myListener` is the callback function. – paddotk Jul 27 '22 at 13:24
  • Is there any workaround for something that's not possible? No. It's not possible. You could theoretically have your popup post a message back to your main window once it is open, and your main window could set focus on itself, but that seems like a lot of work for so little gain. – Heretic Monkey Jul 27 '22 at 13:30
  • Does this answer your question? [Open new tab in background leaving focus on current tab - Chrome](https://stackoverflow.com/questions/51760490/open-new-tab-in-background-leaving-focus-on-current-tab-chrome) – Heretic Monkey Jul 27 '22 at 13:30
  • @HereticMonkey - it's not answering as I'm using mainly Opera, however I would prefer to have a script solution without additional extensions/files. There must be a way to bypass this focusing... – theboldsammer Jul 27 '22 at 18:58

1 Answers1

0

It's maybe worth noting that ctrl-click does not have the same behavior on all platforms; even if this worked, it would only work for a subset of users. (The MDN docs contain a fun example of this; they do note which literal button Ctrl maps to on a mac... but the demo is unusable because ctrl-click triggers a (native, non-browser) context menu, preventing the click event even reaching the demo panel. (For OSX you'd want to look for metaKey, not ctrlKey.)

If you reeeeeeeally want to do this, check navigator.platform first to determine whether to use ctrlKey or metaKey (and possibly something else on other platforms than desktop mac or windows, I'm not certain.)

To open new tabs in the background, I believe you can trigger a focus event on the originating window after the secondary tab/window is opened. (I would strongly recommend against it, though; modifying expected, standard browser behavior is not great UX, and would likely have accessibility implications.)

Daniel Beck
  • 20,653
  • 5
  • 38
  • 53