And the only way to achieve this is by having the mouse interact with an actual link element (HTML <a>, HTML <area> or SVG <a>) with a meaningful href.
A few times I’ve encountered sites trying to be clever and detect Ctrl+click or middle-click and use window.open(), but this doesn’t behave natively: natively, middle-click or Ctrl+click probably open in a background tab, but open() opens a foreground tab, which will always be disruptive for people that use this stuff much, and sometimes infuriating.
This isn’t the only reason for having real links; there are also things like status bar text on hover, and context menu on right click or long press, which may include options like Open Link in New Private Window that cannot be emulated in any way.
If you want to intercepting link click events, you must check if the click is modified in some way like this:
Maybe this should be supported by browsers though. The main place where I hate not having modals is when filling out forms and checking for a slightly expanded explanation about what is expected to be filled into a field. What would be the best behavior for those is if a click showed something inline and middle click (or right click -> open in new tab) opened a new page with the correct tab focus behavior.
This is what can be accomplished by the is-click-modified code I listed. If the click is modified, let the event go through and the browser do whatever with it, but if it’s not modified, call event.preventDefault() to block navigation, then do what you want with it.
A few times I’ve encountered sites trying to be clever and detect Ctrl+click or middle-click and use window.open(), but this doesn’t behave natively: natively, middle-click or Ctrl+click probably open in a background tab, but open() opens a foreground tab, which will always be disruptive for people that use this stuff much, and sometimes infuriating.
This isn’t the only reason for having real links; there are also things like status bar text on hover, and context menu on right click or long press, which may include options like Open Link in New Private Window that cannot be emulated in any way.
If you want to intercepting link click events, you must check if the click is modified in some way like this:
Note that middle-click fires an auxclick event instead of click, and right-click fires contextmenu first and then auxclick if that’s preventDefaulted.