-2

It is quite simple to register a click event and determine if an A-element has been left clicked:

window.addEventListener('click', ()=>{
    if (event.which !== 1 && event.which !== 2) // left of middle click
        return
    var element = event.target
    if (element.tagName !== 'A') {
        while(element.parentNode) {
            element = element.parentNode
            if (element.tagName === 'A')
                break
        }
        if (element.tagName !== 'A')
            return
    }
    console.log('A-Element was left clicked')
})

But my code can't detect it when the user opens a link via right click->open link or when a link is opened using JavaScript (window.open(), location.href, ...).

Is right click a Javascript event? doesn't solve my problem, because right clicks themselves don't open links.

So, I'm wondering if there is a good way to find out whenever a link is opened, not only if it is clicked.

I've found browser.tabs.onUpdated which is an event that gets fired whenever a tab is updated. It's extension-exclusive though.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Forivin
  • 14,780
  • 27
  • 106
  • 199
  • Possible duplicate of [Is right click a Javascript event?](http://stackoverflow.com/questions/2405771/is-right-click-a-javascript-event) – Tushar Arora Jan 17 '17 at 20:53
  • You can use the `contextmenu` event to detect when they right-click on it. I don't think there's any event triggered if a JS method is called on the link. – Barmar Jan 17 '17 at 20:53
  • I don't need an event for when the context menu opens. I need an event for when an entry like "Open link ..." is clicked in a context menu. Or maybe a general even for when a link is being opened. – Forivin Jan 17 '17 at 20:57
  • 1
    There is no such event fired by browsers. – Pointy Jan 17 '17 at 21:00
  • @Pointy Are you 100% certain? – Forivin Jan 17 '17 at 21:20
  • The only way to achieve this is to disable the actual right click, and build a custom right click dialog instead, under your own control. Catching the "open link" with JS is simply not possible. – Shadow The GPT Wizard Jan 27 '17 at 23:14
  • Also, same question but with jQuery: http://stackoverflow.com/questions/15944572/detect-right-clicks-open-in-new-tab-on-link-in-jquery – Shadow The GPT Wizard Jan 27 '17 at 23:19

2 Answers2

1

This might get you close, but it won't handle all cases, such as if they right-click and open in new window or new tab. Basically this sample WON'T work if you open anything in a new tab. But it will work if you open anything in the current tab/window regardless of how it is done (right-click, window.open). Its the window.onbeforeunload event. It fires before navigating AWAY from the current page.

window.onbeforeunload = function () {
    alert("Navigating away...");
};
Matt Spinks
  • 6,380
  • 3
  • 28
  • 47
  • Thanks. Well it is something that I'll add to my code. But another limitation that I see is that it can't handle protocols other than http and https. It wouldn't work for a mailto link for instance. – Forivin Jan 17 '17 at 21:16
0

Hmm not sure this would be the best answer but with jQuery's event.which it's possible:

$('#element').mousedown(function(event) {
    switch (event.which) {
        case 1:
            alert('Left click pressed.');
            break;
        case 2:
            alert('Middle button pressed.');
            break;
        case 3:
            alert('Right click pressed.');
            break;
        default:
            alert('default');
    }
});

Snippet below:

$('#link').mousedown(function(event) {
  switch (event.which) {
    case 1:
      alert('Left click pressed.');
      break;
    case 2:
      alert('Middle button pressed.');
      break;
    case 3:
      alert('Right click pressed.');
      break;
    default:
      alert('default');
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<a href="whatever.com" id="link">click to find out</a>
Syden
  • 8,425
  • 5
  • 26
  • 45
  • This does get you into the handler code when the right mouse button is clicked, but it doesn't intercept a subsequent click on the "Open in new tab" menu entry. – Pointy Jan 17 '17 at 21:05
  • I didn't tag the question with jquery. Besides that it is just a click handler. The same thing can be done with the code found in the question. – Forivin Jan 17 '17 at 21:19
  • Forivin pardon me for suggesting an alternative, I'll be here or in my grave when you find the answer you look for. – Syden Jan 17 '17 at 21:23
  • Well, if I would have wanted a jquery answer or anything else, the question would be tagged accordingly. Maybe you should read the javscript tag, as it clearly sates "Unless a tag for a framework or library is also included, a pure JavaScript answer is expected for questions with the javascript tag". – Forivin Jan 17 '17 at 21:27
  • Well, if people read tags instead of question descriptions, your votes would be inverse. I'll take a tag reading course when I get a chance, thanks for the advice. – Syden Jan 17 '17 at 22:07
  • No, the problem is that some people don't read question descriptions. I literally had questions going from 0 to -19. Then I added "Please read the whole question before voting" to the top and after a week it went up to 0 again. ... But as I said, your answer wouldn't even be helpful if I had included the jquery tag because I literally use event.which in the code in my question. And catching a right click event wouldn't help, as right clicks do not open links. If you read the question description, you should have known that I'm looking for events other than click. – Forivin Jan 18 '17 at 09:16