A link can be launched in several ways (if using modern browsers, see note):
- Left click
- For
<a target="_blank"> or <a target="_new"> page will be loaded into a new tab . - When the user presses CTRL , the page will be loaded into a new tab .
- When the user presses SHIFT , the page will be loaded in a new window .
- For other
target values, the link can be loaded into the current screen or another frame .
- Midde click
- The page will be loaded into a new tab, regardless of the modifier keys.
- Right click
This method is incredibly complicated, if not impossible, to fully implement .
JavaScript cannot be used to directly determine which option is selected in the context menu.- If you select "Open link in new (tab, window)", the page will load into a new tab, window.
- If you donβt select any of them, nothing loads
- Keyboard:
- Context menu (see 3.)
- Enter - see 1.
How to capture these links
There is no reliable way to capture all link events.
- The
onmouseup event can be used to detect 1, 2, and 3.
The button used can be tracked using the event.button (or event.which ) event.which . In all non-IE browsers, 0 = left, 1 = middle, 2 = right. In IE, 1 = left, 2 = middle, 4 = right. - The
onclick event can be used to capture 1 and 2.
The modifier key can be detected through the properties event.altKey and event.shiftKey . oncontextmenu event can be used to capture 3- The
onkeydown event can be used to capture 4.
The behavior of modifier keys is not formalized in any specification and, therefore, depends on the browser. At least Firefox (at least 3+) and Chrome (all?) Implement this key behavior.
Related question - How to intercept the action on the link ( <a> )?
Rob w source share