1

I've just written some code, that when the user clicks on a link (I'm using jQuery's .click()), they get a modal popup telling them they're being redirected to a different site. This all works fine with a normal left click, however, clicking the link using the middle mouse button to open it in a different tab loads the page straight away rather than showing the modal. Ideally I'd like to show them the modal too, then after the timer finishes open it in a new tab for them.

Is it possible to capture this click too?

TMH
  • 6,096
  • 7
  • 51
  • 88

4 Answers4

0

Try this, It will help

$("yourtag").on('mousedown', function(e) { 
   if( (e.which == 1) ) {
     alert("left button");
   }if( (e.which == 3) ) {
     alert("right button");
   }else if( (e.which == 2) ) {
      alert("middle button"); 
   }
   e.preventDefault();
}).on('contextmenu', function(e){
 e.preventDefault();
});
Ganesh Gaxy
  • 657
  • 5
  • 11
0

Yes it is possible through simple click event listener:

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

just for your information, e.which == 1 will fire for left click and 3 for right click.

Abdul Jabbar
  • 2,573
  • 5
  • 23
  • 43
0

Try this code,

$(document).on("mousedown", "a", function(e) {
   if( e.which == 2 ) {
      e.preventDefault();
      //your code here
   }
});
Chamika Sandamal
  • 23,565
  • 5
  • 63
  • 86
  • 2
    Please add some explanation to your code. E.g. `.on(event, selector, handler)` is only helpful if you explain it, because `.on()` is seldom used in examples with more than two arguments. – Kijewski Aug 18 '14 at 12:32
0

Refer this answer : Live demo

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

Ref

Community
  • 1
  • 1
Naveenkumar
  • 483
  • 5
  • 20