Detect middle button click (scroll button) with jQuery
Please don't fire click actions during the mousedown
event. It ignores the rest of the event pipeline and goes against current user expectations and design practices.
Problem
Here's a look at the normal order of events that fire for each click type:
$(document).on("mousedown mouseup click focus blur",function(e) {
console.log("{" + e.which + ":" + e.type + "}");
});
Typically, we handle the very last click
event because it signifies the users final intent to proceed with the current action.
The
click
event is fired when a pointing device button (usually a mouse's primary button) is pressed and released on a single element.
Unfortunately, middle mouse presses do not fire such an event (probably because doing so would force developers listening to the click event to distinguish between multiple possible invocations). But if we want to wire up actions against the middle click, we should follow the same UX expectations.
Solution
We'll listen for mousedown
events and immediately attach a one time use handler for mouseup
events. If the middle key was pressed and if the elements match, we'll trigger our own custom event with type middleclick
, which we'll seed from the original event.
$(document).on("mousedown", function (e1) {
$(document).one("mouseup", function (e2) {
if (e1.which == 2 && e1.target == e2.target) {
var e3 = $.event.fix(e2);
e3.type = "middleclick";
$(e2.target).trigger(e3)
}
});
});
That'll help separate out determining if the middle button was clicked with how we we want to handle it in that case, so we can setup a listener for our custom event type like this:
$(document).on("middleclick", function (e) {
console.log("{" + e.target.nodeName.toLowerCase() + ":" + e.type + "}");
});
Working Demo in jsFiddle and Stack Snippets:
$(document).on("mousedown", function (e1) {
$(document).one("mouseup", function (e2) {
if (e1.which == 2 && e1.target == e2.target) {
var e3 = $.event.fix(e2);
e3.type = "middleclick";
$(e2.target).trigger(e3)
}
});
});
$(document).on("middleclick", function (e) {
console.log("{" + e.target.nodeName.toLowerCase() + ":" + e.type + "}");
});
$(document).on("mousedown mouseup click focus blur",function(e) {
console.log("{" + e.which + ":" + e.type + "}");
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.js"></script>
<div style="background:#31965a;color:white;width:100px;height:100px;line-height:100px;text-align:center;">
Click Me!
</div>
* Tested in Chrome, FF, Edge, & IE11
Ok, I think I've got it. Here's a fiddle that seems to work. The trick (at least with FF4) seems to be to bind a click handler to the document
and have it stop the propagation.
$(document).click(function(e){
//not checking for the right click will prevent the context-menu from showing, you may or may not want to do that
if (e.which != 3) {
e.preventDefault();
return false;
}
});
This solution was found on this forum page.
Okay guys,
Thank you for your input. '@no.good.at.coding' has a nice solution but does not work on IE (see later on this writing) but is a good starting point. I change his code to this:
// Avoid relations will be opened in extra tab when clicking middle-scroll-button to open it
$(document).bind($.browser.msie ? "mousedown" : "click", function(e)
{
if (e.which == 2 && e.target.tagName == 'A')
{
var bIE = $.browser.msie,
$o = $(e.target),
oe = $o.data('events'),
b = true,
f = function(){};
if (typeof oe == 'object' && oe['click']) {
f = function(){ $o.trigger('click'); }
} else {
b = (typeof $o[0].href == 'string' && ($o[0].target == undefined || $o[0].target == '' || $o[0].target == '_self'));
if (b) {
f = function () { window.location.href=$o[0].href; };
}
}
if (!b) { return; }
e.stopImmediatePropagation();
e.stopPropagation();
e.preventDefault();
if (bIE)
{
if (!window.__swcp__) {
window.__swcp__= $('<div style="position:fixed;top:0px;left:0px;width:100%;height:100%;background:#000;display:block;z-index:9999;opacity:0.01;filter:alpha(opacity:1);" />').appendTo('body');
}
window.__swcp__.show();
}
setTimeout(f, 50);
if (bIE) {
setTimeout( function() { window.__swcp__.hide(); }, 1000 );
}
return false;
}
});
The middle-button acts now like the left-button of a mouse. As you can see IE needs some more, we must unfocus the link tag for 1 second to avoid that it will be opened in an extra tab. I use here a simple way to do this, create a 'for the eye" transparent div (opacity = 0.01) that will be placed over the window content so any link on the page will be unfocused. The user will not notice any difference except that the mouse pointer can change from pointer to default and after one second it change back to pointer (IE only). I can live with that.
I accept this as an answer, but if you have better idea's let me know.
Well after a quick test it seems that the three are ordered as follows:
- Left - 1
- Middle - 2
- Right - 3
So if you had:
$(document).mousedown(function(e){
switch(e.which)
{
case 1:
//left Click
break;
case 2:
//middle Click
break;
case 3:
//right Click
break;
}
return true;// to allow the browser to know that we handled it.
});