]> git.ardis.lu - minimal-routing/commitdiff
Update names to reflect what they're doing
authorArdis Lu <ardisglu@gmail.com>
Tue, 1 Feb 2022 05:03:28 +0000 (21:03 -0800)
committerArdis Lu <ardisglu@gmail.com>
Tue, 1 Feb 2022 05:03:28 +0000 (21:03 -0800)
index.html

index 46dcce4a373bf3eefe8426f731d85f8662ee2ae3..450957a1beefa41c311e24d30e47452d27ee449f 100644 (file)
@@ -37,8 +37,8 @@
     const main = document.querySelector('main');
     const pages = Array.from(document.querySelectorAll('[data-page]')); // Must convert to array to preserve elements
 
-    // Display the appropriate page based on the URL path
-    function route(path) {
+    // Display the appropriate page based on the URL path (does NOT modify browser history)
+    function showPage(path) {
       if (path === '') {
         path = 'home';
       }
@@ -60,8 +60,8 @@
       }
     }
 
-    // Update the browser history, then route
-    function changePage(path) {
+    // Update the browser history, then show page
+    function route(path) {
       // If the new page is equal to the current page, don't do anything
       if (window.location.pathname.substring(1) === path) {
         return;
       const url = new URL(path, base);
       window.history.pushState({}, '', url);
 
-      route(path);
+      showPage(path);
     }
 
     // Set event handlers
     for (const router of routers) {
-      router.addEventListener('click', e => changePage(e.target.dataset.route));
+      router.addEventListener('click', e => route(e.target.dataset.route));
     }
-    window.onpopstate = _ => route(window.location.pathname.substring(1));
+    window.onpopstate = _ => showPage(window.location.pathname.substring(1));
 
-    // Route to the current URL (to support direct linking aka deep links)
-    route(window.location.pathname.substring(1));
+    // Show the current URL (to support direct linking aka deep links)
+    showPage(window.location.pathname.substring(1));
   </script>
 </body>
 
This page took 0.034781 seconds and 4 git commands to generate.