Hash-Based Routing
What you'll learn: How to build simple client-side routing using URL hash fragments without triggering full page reloads.
The Core Idea
Hash-based routing uses the part of the URL after the # symbol (the hash fragment) to represent different "pages" or views in your single-page application. When you change example.com/#/home to example.com/#/about, the browser doesn't reload the page—it just updates the hash and triggers a hashchange event you can listen to.
Think of it like bookmarks in a long document. The document itself doesn't change, but clicking different bookmarks scrolls you to different sections instantly.
How It Works
The hash fragment was originally designed for linking to sections within a page (#section-id). Clever developers realized they could use it for routing:
- User clicks a link that changes the hash:
#/profile - Browser updates
window.location.hashwithout reloading - Your code listens for the
hashchangeevent - You read the new hash value and update the page content accordingly
This creates the illusion of navigation without server requests.
Why Use Hash Routing?
Before the History API (which you learned about), hash routing was the only way to change URLs client-side. It requires zero server configuration—any web server can handle it because the server never sees the hash fragment (it's not sent in HTTP requests).
The Limitations
Hash routing has significant drawbacks in modern apps:
- URLs look less clean:
example.com/#/products/123vsexample.com/products/123 - SEO challenges: search engines historically ignored hash fragments
- The hash was meant for page anchors, not routing (semantic mismatch)
- Can't use the full History API features like state objects with navigation
Key Takeaway: Hash-based routing enables client-side navigation without server configuration by leveraging URL fragments and the hashchange event, but modern applications typically prefer History API routing for cleaner URLs and better functionality.