118

I am using the onclick event of a hashed link to open a <div> as a pop up. But the middle click does not trigger the onclick event but only takes the href attribute value of the link and loads the URL in a new page. How can I use middle click to open the <div> as a popup?

Sk8erPeter
  • 6,899
  • 9
  • 48
  • 67
Sadesh Kumar N
  • 1,962
  • 7
  • 19
  • 26
  • 18
    I still don't understand how the problem was solved, when middle click does not trigger the onclick event. – Tomáš Zato Aug 31 '13 at 14:45
  • 4
    @TomášZato Browsers don't fire a 'click' event with a middle-click, but they might fire a 'mouseup' event. Then the javascript framework may bind this to a 'click' action to confuse you. I recommend the reading of http://www.unixpapa.com/js/mouse.html – rds Sep 18 '13 at 18:29

8 Answers8

71

EDIT

This answer has been deprecated and doesn't work on Chrome. You will most probably end up using the auxclick event, but please refer to other answers below.

/EDIT


beggs' answer is correct, but it sounds like you want to prevent the default action of the middle click. In which case, include the following

$("#foo").on('click', function(e) {
   if (e.which == 2) {
      e.preventDefault();
      alert("middle button"); 
   }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<a id="foo" href="http://example.com">middle click me</a>

preventDefault() will stop the default action of the event.

golopot
  • 10,726
  • 6
  • 37
  • 51
Gausie
  • 4,291
  • 1
  • 25
  • 36
  • 20
    Keep in mind that `.live` was deprecated and removed in favor of `.on` – Naftali Jul 16 '13 at 15:29
  • To get this working in FF use: var evt = e || window.event; – Jonathan Sep 04 '15 at 06:23
  • 2
    For me FF works only with mousedown event. Click event calls this thing http://scr.hu/1kig/su5c9 – l00k Dec 18 '15 at 23:11
  • 1
    Is there any cross-browser constant for that `2`? – fracz Feb 08 '16 at 21:21
  • @fracz there is a standard, but ofc IE doesn't follow it - middle button has id=4, and right button has id=2. Read about it [here](http://stackoverflow.com/questions/10562950/mousemove-and-cross-browser-e-which) and [here](http://www.martinrinehart.com/early-sites/mrwebsite_old/examples/cross_browser_mouse_events.html). Check if libraries you use normalize those IDs. – pzmarzly Feb 23 '16 at 20:58
  • @xcy7e웃 - it's not working for me. What is working, though, is changing the eventhandler to either 'mousedown' or 'mouseup'. Tested with FF, Chrome and Edge: Left click: e.which -> 1; e.button: 0 Middle click: e.which -> 2; e.button: 1 Right click: e.which -> 3; e.button: 2 ======= IE8: 1/1; 2/4; 3/2 – retrovertigo Jun 01 '16 at 03:12
  • 11
    This no longer works in Chrome 55. See [1](https://bugs.chromium.org/p/chromium/issues/detail?id=255#c160), [2](https://developers.google.com/web/updates/2016/10/auxclick) and [this question](http://stackoverflow.com/q/41110264/499922) – billc.cn Dec 19 '16 at 11:41
  • 1
    How is this a solution when the problem was that middle click does not trigger the click event to begin with? – Drazen Bjelovuk Jul 13 '17 at 14:58
  • 1
    .which is deprecated, use .button Not sure who named it "which" in the first place. I don't know what web devs were doing 10-15 years ago... – Bojidar Stanchev Jul 09 '19 at 11:53
  • LET IT CROSS-BROWSERS: `...function(e) { e = e || window.event; var b = e.keyCode || e.which; if (b == '2') {...` – L777 Sep 23 '19 at 00:11
  • 1
    This answer should not be pinned to the top as it is out-of-date and it doesn't work. – Flimm Mar 04 '20 at 16:51
64

For the middle-click / mouse-wheel button to be detected, you have to use the event auxclick. E.g:

<a href="https://example.com" onauxclick="func()" id="myLink"></a>

Then in your script file

function func(e) {
  if (e.button == 1) {
    alert("middle button clicked")
  }
}

If you want to do it from JavaScript (without using the HTML attribute onauxclick), then you addEventListener to the element:

let myLink = document.getElementById('myLink')
myLink.addEventListener('auxclick', function(e) {
  if (e.button == 1) {
    alert("middle button clicked")
  }
})
<a id="myLink" href="http://example.com">middle click me</a>

Checkout the mdn page about the auxclick event here.

Flimm
  • 136,138
  • 45
  • 251
  • 267
Hassan
  • 946
  • 8
  • 6
  • note: onauxclick will work but it does not open the link in a new tab... – Jonathan Laliberte Jun 06 '18 at 17:40
  • 4
    This is the only answer that actually works in Chrome. If you need a "click" event for the right mouse button, you can use "contextmenu" (although it's not only fired by right click): https://developer.mozilla.org/en-US/docs/Web/API/Element/contextmenu_event – Kankaristo Apr 13 '19 at 18:28
  • 2
    combining with above answer, you can prevent right click capture by `if(event.button==1)` clause, this is the only working answer. – Buddy Sep 15 '19 at 13:18
  • 1
    Note that you need to use both the `auxclick` event, and to check for the value of `e.button == 1`. I edited the answer. – Flimm Mar 04 '20 at 17:06
  • Note that it does not work on Safari. https://caniuse.com/auxclick – LucianDex Aug 09 '22 at 07:08
28

You can use

event.button

to identify which mouse button was clicked.

Returns an integer value indicating the button that changed state.

  • 0 for standard 'click', usually left button
  • 1 for middle button, usually wheel-click
  • 2 for right button, usually right-click

Note that this convention is not followed in Internet Explorer: see QuirksMode for details.

The order of buttons may be different depending on how the pointing device has been configured.

Also read

Which mouse button has been clicked?

There are two properties for finding out which mouse button has been clicked: which and button. Please note that these properties don’t always work on a click event. To safely detect a mouse button you have to use the mousedown or mouseup events.

document.getElementById('foo').addEventListener('click', function(e) {
  console.log(e.button);
  e.preventDefault();
});
<a id="foo" href="http://example.com">middle click me</a>
Flimm
  • 136,138
  • 45
  • 251
  • 267
rahul
  • 184,426
  • 49
  • 232
  • 263
  • 4
    I'd recommend using `event.which` as it has been standardized across browsers in the jQuery source - line 2756 - `if ( !event.which && event.button ) event.which = (event.button & 1 ? 1 : ( event.button & 2 ? 3 : ( event.button & 4 ? 2 : 0 ) ));` – Russ Cam Nov 25 '09 at 10:04
  • 2
    @RussCam However, `MouseEvent.which` is not defined in any spec, but `MouseEvent.button` is defined in DOM L2 Events. – Oriol Jan 12 '15 at 02:41
  • @Oriol correct, but implemented differently across different browser vendors. The OP tagged the question with jQuery, so that's why I suggested using `event.which` but should have elaborated that `MouseEvent.which` is not part of the official specification. – Russ Cam Jan 12 '15 at 02:52
  • @rahul, What about gaming mouses with more than 7 buttons? http://www.egg-tech.com/blog/images/mouse%20buttons.jpg – Pacerier Aug 11 '17 at 22:41
  • 1
    Actually, this doesn't work in browsers like Firefox or Chrome, only for primary click. See [1](https://bugs.chromium.org/p/chromium/issues/detail?id=255#c160), [2](https://developers.google.com/web/updates/2016/10/auxclick) and [this question](https://stackoverflow.com/q/41110264/247696). – Flimm Mar 04 '20 at 16:56
13

This question is a bit old, but i found a solution:

$(window).on('mousedown', function(e) {
   if( e.which == 2 ) {
      e.preventDefault();
   }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<a href="http://example.com">middle click me</a>

Chrome not fire "click" event for the mouse wheel

Work in FF and Chrome

Flimm
  • 136,138
  • 45
  • 251
  • 267
Bastien Bast Weber
  • 453
  • 2
  • 5
  • 12
  • I don't want to assume your use case, but I would bet money a lot of people would actually need the 'mouseup' event instead. It works with the middle mouse button as well. – Bojidar Stanchev Jul 09 '19 at 14:06
  • When I try this in Firefox and Chrome, it does not prevent the default behaviour of opening the link in a new tab, even though `e.preventDefault()` is called. – Flimm Mar 05 '20 at 08:24
8

I usually hate when people offer alternatives instead of solutions, but since solutions have already been provided I'm going to break my own rule.

Websites where the middle-click feature is overridden tend to really, really bug me. I'm usually middle-clicking because I want to open the new content in a new tab while having an unobstructed view of the current content. Any time you can leave the middle-click functionality alone and make the relevant content available through the HREF attribute of your clicked element, I strongly believe that's what you should do.

Tom Haflinger
  • 105
  • 1
  • 2
  • 2
    but what if you have tabs on your webpage and want the same behaviour as in your browser? middleclick should close an existing tab then. – cmxl Dec 11 '18 at 15:40
7

jQuery provides a .which attribute on the event that gives the click button id from left to right as 1, 2, 3. In this case you want 2.

Usage:

$("#foo").live('click', function(e) { 
   if( e.which == 2 ) {
      alert("middle button"); 
   }
}); 

Adamantium's answer will also work but you need to watch out for IE as he notes:

$("#foo").live('click', function(e) { 
   if((!$.browser.msie && e.button == 1) || ($.browser.msie && e.button == 2)) { 
     alert("middle button"); 
   } 
});

Also remember the .button attribute is 0-indexed not 1-indexed like .which.

beggs
  • 4,185
  • 2
  • 30
  • 30
  • can i override the functionality of mozilla browser!! eg on left click it opens a pop up.... but for middle click it is not opening a pop up and a new tab is opened in which the pop up is not opening.. so i want to override a middle click functionality of mozilla.... – Sadesh Kumar N Nov 25 '09 at 10:32
  • Mozilla has a setting to open all popups in a new tab rather than a new window. If the user has this option enabled then I don't *think* there is anything you can do. If this is not what is happening can you post some code or a link to the problem page? – beggs Nov 25 '09 at 11:56
  • This doesn't work in browsers like Firefox and Chrome. – Flimm Mar 04 '20 at 16:58
5

The proper method is to use .on, as .live has been deprecated and then removed from jQuery:

$("#foo").on('click', function(e) { 
   if( e.which == 2 ) {
      e.preventDefault();
      alert("middle button"); 
   }
});

Or if you want the "live" like feel and #foo is not on your page on document start:

$(document).on('click', '#foo', function(e) { 
   if( e.which == 2 ) {
      e.preventDefault();
      alert("middle button"); 
   }
});

original answer

Community
  • 1
  • 1
Naftali
  • 144,921
  • 39
  • 244
  • 303
  • 1
    The event `click` does not work for the middle mouse button in browsers like Firefox and Chrome. – Flimm Mar 04 '20 at 17:00
  • it used to @Flimm – Naftali Mar 04 '20 at 21:09
  • Yeah. Stack Overflow does not have a good solution for out-of-date answers. I am afraid I have to downvote because "This answer is not useful" any more. What is worse is that the pinned answer can have hundreds or thousands of downvotes because it is out-of-date, and it will still stay pinned forever if the OP is not responding. – Flimm Mar 05 '20 at 08:18
  • Ask a new question :-) (reference this one saying it is out of date and you want a 2020 answer) . But also can use mousedown/mouseup events maybe? – Naftali Mar 05 '20 at 14:38
  • Even if I do that, people like me are still going to come across this answer when coming from search engines, and are going to be confused by the pinned wrong answer. – Flimm Mar 05 '20 at 14:42
  • I can link it in my answer when you have yours (for a as of 2020 go here or something like that :-) ) – Naftali Mar 05 '20 at 19:44
3

I know I'm late for the party, but for those still having problems with handling the middle click, check if you delegate the event. In case of delegation, the click event does not fire. Compare:

This works for middle clicks:

$('a').on('click', function(){
  console.log('middle click');
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<a href="http://example.com">middle click me</a>

This doesn't work for middle clicks:

$('div').on('click', 'a', function(){
  alert('middle click');
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div>
  <a href="http://example.com">middle click here</a>
</div>

If you still need to track the middle click using event delegation, the only way around as stated in the corresponding jQuery ticket, is to use mousedown or mouseup instead. Like so:

This works for delegated middle clicks:

$('div').on('mouseup', 'a', function(){
  console.log('middle click');
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div>
  <a href="http://example.com">middle click me</a>
</div>

See it online here.

Flimm
  • 136,138
  • 45
  • 251
  • 267
skip405
  • 6,119
  • 2
  • 25
  • 28