]> git.ardis.lu - minimal-routing/commitdiff
Replace buttons with anchors
authorArdis Lu <ardisglu@gmail.com>
Fri, 30 Sep 2022 04:10:35 +0000 (21:10 -0700)
committerArdis Lu <ardisglu@gmail.com>
Fri, 30 Sep 2022 04:10:35 +0000 (21:10 -0700)
More semantically accurate and allows link preview on hover

index.html

index 8882e6a7587a1d4943fbd4c1be04380a35d3a1ca..d4cf2cb610d598707780e0c56ca82b7c5ff291c4 100644 (file)
 
 <body>
   <header>
-    <button data-router data-route="home">Go to the home page</button>
-    <button data-router data-route="page_1">Go to page 1</button>
-    <button data-router data-route="page_2">Go to page 2</button>
-    <button data-router data-route="page_3">Go to page 3</button>
+    <a data-router data-route="home" href="/home">Go to the home page</a>
+    <a data-router data-route="page_1" href="/page_1">Go to page 1</a>
+    <a data-router data-route="page_2" href="/page_2">Go to page 2</a>
+    <a data-router data-route="page_3" href="/page_3">Go to page 3</a>
   </header>
 
   <main>
 
     // Set event handlers
     for (const router of routers) {
-      router.addEventListener('click', e => route(e.target.dataset.route));
+      router.addEventListener('click', e => {
+        e.preventDefault();
+        route(e.target.dataset.route)
+      });
     }
     window.onpopstate = _ => showPage(window.location.pathname.substring(1));
 
This page took 0.038018 seconds and 4 git commands to generate.