0

This is the code I'm using as this moment for the fade out effect on all links with the class "link"

$('.link').click(function() {

event.preventDefault();

newLocation = this.href;

$('body').fadeOut(700, newpage);

});

The problem is that the middle click and/or open in a new tab doesn't work. Is there a way I can change this code so that my users both can right-click and/or middle click on these links?

levi
  • 22,001
  • 7
  • 73
  • 74
QAW
  • 295
  • 2
  • 3
  • 9
  • Your question is vague. What is the expected (desired) action to take place when the user right clicks or middle clicks? See if this helps: http://stackoverflow.com/questions/1206203/how-to-distinguish-between-left-and-right-mouse-click-with-jquery – Jason Aug 05 '14 at 18:31

2 Answers2

3

to detect which from what click the event is comming, you can use that code:

$(document).mousedown(function(e){
    switch(e.which)
    {
        case 1:
            //left Click
        break;
        case 2:
            event.preventDefault();

            newLocation = this.href;

            $('body').fadeOut(700, newpage);
            break;
        case 3:
            //right Click
        break;
    }
    return true;// to allow the browser to know that we handled it.
});
levi
  • 22,001
  • 7
  • 73
  • 74
  • Question, how are you switching (e.which)? I know that for keydown, e.which will return the numerical value of the key, but what does e.which return for mousedown? – Charles Aug 05 '14 at 18:39
  • mousedown return the event (e) then if you use e.which get the numerical value of the key and then it will enter in the case 2, if you have pressed the middle click. – levi Aug 05 '14 at 18:42
  • @Charles It has 3 possible number to return, 1 left click, 2 middle click or 3 right click. – Wilfredo P Aug 05 '14 at 18:42
  • Niiiice, I love learning something new everyday. Thanks for the info guys! – Charles Aug 05 '14 at 18:44
-1
$('.link').click(function(e) {
  var clicked = e.which;
  // 1 = left click, 3 = right click
  if (clicked !== 1 || clicked !== 3) {
    e.preventDefault();
    newLocation = this.href;
    $('body').fadeOut(700, newpage);
  }
});
Ross Joo
  • 180
  • 6