From 2ba64d17c12a1f3e7ac104fd31aa5c2a09cc392f Mon Sep 17 00:00:00 2001 From: Ardis Lu Date: Tue, 29 Oct 2024 20:00:55 -0700 Subject: [PATCH] Stop event propagation instead of specifically handling it This pattern is more idiomatic and self-contained, which is easier to maintain. --- script.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/script.js b/script.js index 79450b6..ea5336b 100644 --- a/script.js +++ b/script.js @@ -293,6 +293,7 @@ document.addEventListener('keydown', e => { /* Implement "skip to main" button */ document.querySelector('#skip-to-main').addEventListener('click', e => { e.preventDefault(); + e.stopPropagation(); // Block bubbling up to document-wide click handler for client-side routing document.querySelector('main').querySelector('a')?.focus(); // Assumes first focusable element is always }); @@ -396,7 +397,7 @@ function route(href) { /* Set event handlers for client-side routing. */ globalThis.addEventListener('popstate', _ => showPage(location.pathname)); document.addEventListener('click', e => { - if (e.altKey || e.ctrlKey || e.metaKey || e.shiftKey || e.target.id === 'skip-to-main') { + if (e.altKey || e.ctrlKey || e.metaKey || e.shiftKey) { return; // Use default behavior if any keyboard modifier is applied to the click or it's a special link } -- 2.47.3