0

I want to call javascript function when user right click on link and selects "Open link in new tab" and "Open link in new windw" options. Is there any way to do this? I don't want to use "oncontextmenu" and also don't want to customize contextmenu options.

Nitesh
  • 1,477
  • 5
  • 23
  • 34
  • 1
    Strictly speaking, no you can't. But, I was wondering as to why? Because I could easily whip up a PHP-based solution (although it couldn't distinguish between the two). Or, if you're trying to warn the user, we can disable that kind of functionality quite easily. – Neil Mar 10 '17 at 07:43
  • Sorry I miss understood.. no you can't as commented above. – Abhinav Mar 10 '17 at 07:48
  • 1
    Possible duplicate of [how to capture open link in new tab or window using jquery?](http://stackoverflow.com/questions/7844890/how-to-capture-open-link-in-new-tab-or-window-using-jquery) – Abhinav Mar 10 '17 at 07:53

1 Answers1

1

No, not possible. You have only 2 options to capture the user's right click:

  1. Use oncontextmenu event:

    element.addEventListener('contextmenu', function(ev) {
        ev.preventDefault();
        alert('success!');
        return false;  // standard context menu will not pop up
    }, false);
    

OR

<div oncontextmenu="javascript:alert('success!');return false;">
    Lorem Ipsum
</div>
  1. Use mousedown event:

    $('a').mousedown(function(ev){ 
        if(ev.which == 3){   // 3 -> key code for right-click
            // do your thing
            alert("Right btn clicked");
        }
    }
    
Niket Pathak
  • 6,323
  • 1
  • 39
  • 51