Problem
In attempting to remove the click event for non-primary buttons in Chrome as per UI Event Spec it was discovered there is no way for the developers to prevent opening a new tab when the user middle clicks on a link. The default middle click action of “Open new tab” is not formally documented.
Traditionally Chrome and some of the other vendors like Safari and Edge have fired the click event for all buttons (including non-primary). This poses a number of issues with pages executing click event handlers as traditionally javascript hasn’t examined the button field. Likewise other vendors have followed different approaches here. Mozilla dispatches the click event only to the document level nodes.
It is believe that for interoperability and legacy purposes the best approach is to fire the click event only for the primary button and that is reflected in the current UI Events Spec. However some applications may wish to override the default behavior of the UA. Specifically it is desirable to have an event for the middle button and explicitly define the default behavior.
Incomplete Solution
By listening to the mousedown/up events and saving the target in a global variable, it is possible for javascript to dispatch an untrusted click event with the correct button value to the desired target. However this workaround doesn’t seem to work robustly for example when facing stopimmediatepropagation API as other listeners might interfere with these listeners.
Proposal
Introduce a MouseEvent of type “auxclick” (other suggestions are welcome) which is fired for all non-primary buttons. This event is not only dispatched for middle button but also covers click for other buttons. It should be targeted at the node (i.e. common ancestor of mousedown and mouseup targets) and bubble as the click event does.
Click events are special in terms of allowing untrusted default action behavior for legacy purposes. It is believe that untrusted auxclick events should be are treated purely as untrusted and must not run the default action behavior.
Since we are changing the type from click to auxclick but keeping all other semantics the same, we believe only minor changes to applications that are currently using this behavior would be required.
Use Cases
Some of the use cases were mentioned briefly in the problem specification. But beyond listening to the auxclick event and capability to prevent opening a new tab or recording the metrics for opening the links in the new tab, this new event enables developers to also do actions on click for other buttons and expands the number of ways developers can expect the actions of their users.