Is it possible to capture "Open in New Tab" clicked event of context menu using javascript?
Asked Answered
J

2

22

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

Josephinejosephson answered 12/3, 2018 at 5:27 Comment(4)
You refer this post here: #850558Ulterior
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.Josephinejosephson
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 - can you please describe what actual problem you are trying to solve with this?Whitford
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.Josephinejosephson
T
0

No, it is not possible to directly capture the "Open in New Tab" event from the browser's context menu using JavaScript. This is due to security and privacy reasons, as browsers do not expose such low-level interactions to web pages.

Transude answered 30/6, 2024 at 6:27 Comment(0)
C
-4

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

Coupler answered 12/3, 2018 at 6:3 Comment(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.Josephinejosephson
You need to use library to handler your context menu. Try to use this swisnl.github.io/jQuery-contextMenu/demo.htmlCoupler
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. :)Josephinejosephson

© 2022 - 2025 — McMap. All rights reserved.