How to prevent default behavior when middle click on a link (or add support for "auxclick" event)

Confirmed Issue #18351424 • Assigned to Mahesh J.

Details

Author
Oleh S.
Created
Jul 21, 2018
Privacy
This issue is public.
Found in
  • Microsoft Edge
Reports
Reported by 1 person

Sign in to watch or report this issue.

Steps to reproduce

How to prevent default behavior when middle click on a link (or add support for “auxclick” event)

In my Firefox’s & Google Chrome’s extensions I can prevent default behavior when middle click on a link like this:

function onClick(e) {
var url = getLink(e);
if (e.button == 1) { // Middle Click
// prevent opening a link
e.preventDefault();
e.stopPropagation();
// do something with a link
// url …
}
}
if (browser == “chrome”)
document.addEventListener("auxclick", onClick); // for Google Chrome (old “click” event doesn’t prevent opening link with middle click but they added “auxclick” for this)
else if (browser == “firefox”)
document.addEventListener("click", onClick); // for Firefox (still works)
also https://developers.google.com/web/updates/2016/10/auxclick, https://developer.mozilla.org/en-US/docs/Web/Events/auxclick

I’m also trying to do this for my Microsoft Edge’s extension, but seems middle click event for this browser doesn’t work at all:

function onClick(e) {
var url = getLink(e);
if (e.button == 1) { // Middle Click
alert(“hello”); // isn’t working for Microsoft Edge
}
}
document.addEventListener("click", onClick);
So instead of this for Microsoft Edge I use:

document.addEventListener("mousedown", function(e) {
var target = e.target || e.srcElement;
while (target) {
if (target instanceof HTMLAnchorElement)
break;
target = target.parentNode;
}
if (e.button == 1 && target.href != null) {
alert(“hello”); // works when middle click on a link
// but these preventing doesn’t works here:
e.preventDefault();
e.stopPropagation();
// link will still be opened in a new tab
}
});
But this method doesn’t prevent link from opening in a new tab when middle click

How can I make Microsoft Edge behave like Google Chrome or Firefox?

Attachments

0 attachments

    Comments and activity

    You need to sign in to your Microsoft account to add a comment.

    Sign in