1

I have a javascript which suppose to mark a notification as read when a user clicks on it.

The javascript is triggered in an <a> tag, which after the script returns true, redirects the user to another page with target="_blank";

But if the user uses his middle mouse button, or uses his right click -> open in a new tab, It will not trigger the event.

I know I can just make a "link" and not allow open in a new tab options, but I rather not.

Jacob Cohen
  • 1,232
  • 3
  • 13
  • 33

3 Answers3

1

use the mousedown event includes which button was pressed

$("someTagOrID").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"); 
   }
});
user2321864
  • 2,207
  • 5
  • 25
  • 35
0

Source of my answer and your question is a duplicate of it : How to distinguish between left and right mouse click with jQuery

Although the answer from that question that would help you :

As of jQuery version 1.1.3, event.which normalizes event.keyCode and event.charCode so you don't have to worry about browser compatibility issues. Documentation on event.which

event.which will give 1, 2 or 3 for left, middle and right mouse buttons respectively so:

$('#element').mousedown(function(event) {
    switch (event.which) {
        case 1:
            alert('Left Mouse button pressed.');
            break;
        case 2:
            alert('Middle Mouse button pressed.');
            break;
        case 3:
            alert('Right Mouse button pressed.');
            break;
        default:
            alert('You have a strange Mouse!');
    }
});

Originally posted from "tinybyte"

Community
  • 1
  • 1
Pain
  • 185
  • 1
  • 2
  • 14
0

You can simply use mouse up event. Something like this:

   <a href="http://www.google.com" onmouseup="alert('a')">link</a>
Ramin Mir.
  • 784
  • 1
  • 7
  • 18