Disable mouse scroll middle click event with jQuery

inaz :

I wrote the below code for disabling the scroll in the mouse while it is clicked. but my code does not work an when I click with the scroll of my mouse it opens my link.

Here is my code :

$('a').on('mousedown', function(e) {
  if (e.which === 2) {
    console.log('Disabled');
    return false;
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script>
<a href="#dontwanttoopen"> click here </a>

Akhil Aravind :

You have to use auxclick in order to disable this feature. Replace your 'click' with 'auxclick', and add e.preventDefault(), it will work, tested in chrome and FF

$('a').on('auxclick', function(e) {
  if (e.which === 2) {
    e.preventDefault();
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script>
<a href="#dontwanttoopen"> click here </a>

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=11340&siteId=1