-1

I'm working on a page which works fine for now, but I'd like to make it a bit more responsive by using ajax. So I want to call a JS function and staying on the page when left clicking on a link, but when clicking with the middle mouse button (for opening a new tab) I don't want to execute the function but instead open a new tab (like a regular link).

Right now I have something like this:

<a href="someSite.php" id="myLink">Link</a>

and

myLink.onclick = function () {
  doAjax();
  return false;
};

But in this case clicking with the middle mouse button calls the function.

Is there a way without using any button detection and no jQuery?

YPOC
  • 521
  • 4
  • 20
  • possible duplicate of [How to detect middle mouse button click?](http://stackoverflow.com/questions/21224327/how-to-detect-middle-mouse-button-click) – LJᛃ Oct 02 '14 at 00:39
  • You will need to check the button. There is no "middle" click event. – epascarello Oct 02 '14 at 00:46

1 Answers1

0

You'll have to use some jquery,

$("#myLink").on("mousedown", "a.external", function(e) {
        if( (e.which == 1) ) {
                //left click make your ajax call
        }else if( (e.which == 2) ) {
                //middle click open your new tab
                var _window = window.open('your link', '_blank');
                if(_window){
                        _window.focus();
                }else{
                        console.log('popup blocked');
                }
        }
        e.preventDefault();
});

Hope this will help you

Ahmad
  • 477
  • 2
  • 9