18

I know I can use capture the Right Click event by using jQuery's "contextmenu" but my question is, how can I capture the event after the context menu appears i.e. When the user clicks on "Open Link in New Tab" action.

Any help?

Thanks.

enter image description here

pso
  • 819
  • 12
  • 25
  • You refer this post here: https://stackoverflow.com/questions/850058/is-it-possible-to-detect-if-a-user-has-opened-a-link-in-a-new-tab – Gokul Chandrasekaran Mar 12 '18 at 05:48
  • 2
    I referred to the link that you provided and it seems they are dealing with the page after it has been opened in another tab. Like looking at it's history on the page load event. What I need to do is rather capture the event on the same page before opening the new page. – pso Mar 12 '18 at 07:37
  • I don’t think that is possible at all, at least not from JS running in a website context. This smells very [X/Y problem-y](https://meta.stackexchange.com/q/66377/286047) - can you please describe what actual problem you are trying to solve with this? – CBroe Mar 12 '18 at 08:07
  • My situation is that I have a search result page which is in an ASPX application. When the user clicks on a particular link (which appears upon hovering) in the result row from the search result, I need to turn that row into a different color to identify that it has been viewed. A regular click I can handle no problem. However when the user opens this link by using "Open Link In New Tab" context menu, I don't know if I can capture this click. Thanks for introducing me to the X/Y problem though. – pso Mar 12 '18 at 09:24

1 Answers1

-2

I found this solution

<script type='text/javascript'>
jQuery(function($){
    $('a').mousedown(function(event) {
        switch (event.which) {
            case 1:
                //alert('Left mouse button pressed');
                $(this).attr('target','_self');
                break;
            case 2:
                //alert('Middle mouse button pressed');
                $(this).attr('target','_blank');
                break;
            case 3:
                //alert('Right mouse button pressed');
                $(this).attr('target','_blank');
                break;
            default:
                //alert('You have a strange mouse');
                $(this).attr('target','_self"');
        }
    });
});

Here jQuery: Detect Mouse Click and Open Target in New Tab

Ryuk Lee
  • 720
  • 5
  • 12
  • 3
    Your solution allows me to capture the different mouse clicks but it does not help with capturing the event click on the Context Menu that appears after right click. – pso Mar 12 '18 at 06:16
  • You need to use library to handler your context menu. Try to use this https://swisnl.github.io/jQuery-contextMenu/demo.html – Ryuk Lee Mar 12 '18 at 06:47
  • 2
    Thanks for the link Ryuk but this is creating a custom context menu for the user that disables the browser's default right click interaction. I was looking for something to capture the default interaction provided by the browser. :) – pso Mar 12 '18 at 07:35