2

Quick question; I want to open a link in a new tab without focusing on it (just like Ctrl+Click in Firefox), However the ctrlKey: true doesn't seem to be working.

Here's a sample code:

HTML :

<a href="something.com" id="link">Sample link</a>

Javascript :

var evt = new MouseEvent("click", {
    view: window,
    bubbles: true,
    cancelable: true,
    clientX: 20,
    ctrlKey: true,
    }, ele = document.getElementById("link"));
ele.dispatchEvent(evt);

JsFiddle

BaSsGaz
  • 666
  • 1
  • 18
  • 31
  • In Chromium, this is forbidden by pop-under prevention, see https://bugs.chromium.org/p/chromium/issues/detail?id=487919 and https://bugs.chromium.org/p/chromium/issues/detail?id=431335 – Jan Tojnar Sep 09 '22 at 06:48

2 Answers2

0

Just look at this from the other side:

$("#link").attr("target","_blank")[0].click();
nicael
  • 18,550
  • 13
  • 57
  • 90
  • "I want to open a link in a new tab without focusing on it", I want to stay on the same page while opening a link in a new tab, That's why i wanted to use ctrlKey; – BaSsGaz Mar 22 '15 at 15:35
  • That's a browser specific feature, so there is a good chance it won't work everywhere – Stuart.Sklinar Sep 14 '20 at 10:33
0

I know this is an old thread. Did you ever get an answer? I find the ctrlKey is part working; it correctly makes it open in a new tab, but does not correctly keep focus on the original page.

Stephen Todd
  • 365
  • 3
  • 12