I'm trying to find a way of disabling the default action of the mouse wheel button which is to open the link in a new tab.
Is that possible?
If the middle mouse switch has an open spring, carefully bend the spring so it presses up against the wheel a bit harder. Alternatively, push a bit of springy closed-cell plastic foam between the switch and wheel so as to increase the force needed to depress the switch.
Bind a generic click event handler that specifically checks for middle clicks. Within that event handler, call e.preventDefault()
:
$("#foo").on('click', function(e) {
if( e.which == 2 ) {
e.preventDefault();
}
});
Note that not all browsers support preventing this default action. For me, it only works in Chrome. Firefox, Opera and IE9 all do not raise the click event with middle mouse click. They do raise mouseup and mousedown.
This works for me...
$(document).on("mousedown", "selector", function (ev) {
if (ev.which == 2) {
ev.preventDefault();
alert("middle button");
return false;
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With