]> git.ardis.lu - minimal-routing/commitdiff
Delete window references
authorArdis Lu <ardisglu@gmail.com>
Thu, 28 Nov 2024 06:08:39 +0000 (22:08 -0800)
committerArdis Lu <ardisglu@gmail.com>
Thu, 28 Nov 2024 06:08:39 +0000 (22:08 -0800)
The global window object is already implied in code running within a <script> tag

index.html

index d7af3babf575ceca7d6d5e0956b9732d9151e0e7..f1ecd46c12ff698419c3cfb089ab950f406b774c 100644 (file)
       if (!match) {
         // MUST redirect to a page where the web server serves an actual 404 error (i.e. not just 
         // index.html again) otherwise this triggers an infinite loop
-        window.location.href = `${window.location.origin}/errors/NotFound`;
+        location.href = `${location.origin}/errors/NotFound`;
       }
     }
 
     // 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) {
+      if (location.pathname.substring(1) === path) {
         return;
       }
 
-      const url = new URL(path, window.location.origin);
-      window.history.pushState({}, '', url);
+      const url = new URL(path, location.origin);
+      history.pushState({}, '', url);
 
       showPage(path);
       // Must reset scroll otherwise a user entering a long page from another long page will enter midway scrolled
         route(e.target.dataset.route);
       });
     }
-    window.addEventListener('popstate', _ => showPage(window.location.pathname.substring(1)));
+    addEventListener('popstate', _ => showPage(location.pathname.substring(1)));
 
     // Show the current URL (to support direct linking aka deep links)
-    showPage(window.location.pathname.substring(1));
+    showPage(location.pathname.substring(1));
   </script>
 </body>
 
This page took 0.035981 seconds and 4 git commands to generate.