auxclick Covers The Buttons click Skips

click only fires for the primary button; auxclick handles middle and unclaimed secondary presses, which is where custom middle-click link behavior belongs.

JavascriptData Integrity

Code

javascriptsnippet
16 lines
// click fires only for the primary button; auxclick covers auxiliary
// buttons: middle (button 1) and secondary (button 2) presses that the
// context menu did not claim.
document.addEventListener('auxclick', (event) => {
  if (event.button === 1) {
    const anchor = event.target.closest('a[href]');
    console.log(anchor ? `middle click on ${anchor.getAttribute('href')}` : 'middle click outside a link');
    return;
  }
  if (event.button === 2) {
    console.log('unclaimed secondary click, detail:', event.detail);
  }
});

// Middle-click already opens a background tab on links; auxclick is the
// hook for extra behavior — logging, analytics opt-outs, custom routing.

What to notice first — auxclick Covers The Buttons click Skips

  • The import surface (if any) is all standard library, all versioned with the language itself.
  • The snippet leans on document — browser-only surfaces a bare Node process cannot provide.

Using it

The console output uses these labels: unclaimed secondary click, detail: — when the file runs.

Key statements

const anchor = event.target.closest('a[href]');

Interactive Tools for This Topic

FAQ

Does auxclick Covers The Buttons click Skips run in plain Node.js?
No — it touches `document`, which exist only in the browser. Run it in a page (or the DevTools console) instead of a bare Node process.