0

I am trying to fire a mouse event that would mimic Browser's Ctrl+ mouseclick when clicking on a hyperlink to open a new tab but keep current tab selected. Here is my code:

var link = document.createElement('a');
link.href = 'URL';
document.body.appendChild(link);
link.dispatchEvent(new MouseEvent('click', {ctrlKey: true}))

It does open a new tab but also switches to it. Any chance to keep it on the currect tab?

Thanks

Mark
  • 4,535
  • 7
  • 39
  • 76
  • Does this answer your question? [simulate ctrl + click with javascript or jquery (to open a new tab without focus)](https://stackoverflow.com/questions/34255506/simulate-ctrl-click-with-javascript-or-jquery-to-open-a-new-tab-without-focus) – isherwood Feb 02 '22 at 20:58
  • They are showing basically same code. Maybe it did work like I want at some point but not anymore. – Mark Feb 02 '22 at 21:05
  • did you try `window.open(link.href, '_blank')` instead of dispatching event? – vatavale Oct 28 '22 at 11:27

1 Answers1

0

It's default behavior for the browser, you can't change this(

Andrii Svirskyi
  • 376
  • 1
  • 4