Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript prevent middle mouse button from opening link in new tab

On some websites, you can right-click on a link and chose "open in a new tab" and it works fine, but not if one uses the middle mouse button to do so. I encountered this a few times, it's it not too annoying but I'm still curious what causes this behaviour. (About the HOW) Here is a site that behaves this way browsing with Chrome 46: http://ebookfriendly.com/free-public-domain-books-sources/ the html link tags looks normal:

<a title="Feedbooks" href="http://www.feedbooks.com/">⇢ Feedbooks</a>

The cause must be something in the javascript. Any pointers?

like image 533
Angry Coder Avatar asked Oct 25 '25 02:10

Angry Coder


1 Answers

One way to do this is using the auxclick event. (auxclick on MDN)

The following code will prevent the middle click behaviour on the entire page.

window.addEventListener("auxclick", (event) => {
  if (event.button === 1) event.preventDefault();
});
like image 71
Robbendebiene Avatar answered Oct 26 '25 16:10

Robbendebiene



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!