manikandan103222
manikandan103222

Reputation: 47

Disable mouse middle click event with help of jQuery

I am trying to disable mouse middle click event in anchor tag. It works perfectly in Chrome. Here is my code for Chrome implementation:

//prevent mouse wheel middle click for chrome 
$(document).on('auxclick', 'a', function (e) {
  if (e.which === 2) { //middle Click 
          return false;
  }
return true;
});

Note: above the implementation work only for Chrome 55 and their latest version

Reference auxclick documentation for Chrome: https://developers.google.com/web/updates/2016/10/auxclick

auxclick browser compatibility:

Reference Link: https://developer.mozilla.org/en-US/docs/Web/Events/auxclick

enter image description here

enter image description here

Unfortunately i am unable to disable mouse middle click event in rest of browser like Firefox, IE11 & Safari

I am trying to find possibility in rest of browser. Trying mousedown & mouseup event to disable mouse middle click event in rest of browser but those events don't help to achieve my expectation.

Upvotes: 1

Views: 1907

Answers (1)

JasonK
JasonK

Reputation: 5294

The auxclick event is an experimental technology and therefor it has limited browser support.

You could use the mousedown event:

$('button').on('mousedown', function(e) {
    if (e.which === 2) {
      console.log('Disabled');
      return false;
    }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button>Click me</button>

This should work in all major browsers, though I don't like the idea of changing native behavior.

Upvotes: 1

Related Questions