Master HTML Routing Without Framework A Guide To Client Side Navigation

Crafting a dynamic website often requires routing, the process of matching a URL with the correct page or action. Frameworks like React, Angular, or Django handle this seamlessly, but what if you want to implement HTML routing without relying on these? It's possible with a bit of JavaScript, HTML, and a simple understanding of browser history management. Let's explore this route—pun intended.

30 DAY HTML ROAD  MAP 🗾...
30 DAY HTML ROAD MAP 🗾...

Before we dive in, let's understand why you might want to do this. HTML routing allows for cleaner URLs, better user experience, and improved SEO. It also offers a taste of full-stack development—something no framework can take away from you.

an image of a web page with different colors and font on the bottom, including two separate
an image of a web page with different colors and font on the bottom, including two separate

Setting Up: The Basic Structure

First, we need to set up a basic HTML structure with the div elements where our content will dynamically load. Here's a simple setup:

Handful of New Rails 3 Cheat Sheets - ChurchMag
Handful of New Rails 3 Cheat Sheets - ChurchMag

<div id="app"></div>

Pure HTML Routing

HTML Roadmap  #aitechworlds #html #html5 #htmlcss
HTML Roadmap #aitechworlds #html #html5 #htmlcss

While it's possible to implement routing purely with HTML and JavaScript without any libraries, it becomes verbose and repetitive. Still, it's good to understand how it works at its core.

Let's say you have three pages—'home.html', 'about.html', and 'contact.html'. In 'index.html', we'll handle the routing:

  1. Include all three pages in 'index.html'.
  2. Use JavaScript to show/hide these sections based on the URL.
Sql Wallpaper, Javascript Programming Language Facts, Javascript Coding Tips And Tricks, Javascript Coding Best Practices, Learn Javascript Technologies, Advanced Javascript Coding Tips, Coding Wallpaper Programming Iphone, Black Background Code Snippet, Advanced Javascript Coding Guide
Sql Wallpaper, Javascript Programming Language Facts, Javascript Coding Tips And Tricks, Javascript Coding Best Practices, Learn Javascript Technologies, Advanced Javascript Coding Tips, Coding Wallpaper Programming Iphone, Black Background Code Snippet, Advanced Javascript Coding Guide

Leveraging Hashchange Event

Another way to achieve routing is by listening to the hashchange event in JavaScript. This event fires whenever the hash part of the URL changes. We can use this to load different content.

Here's a simple example:

Rotate Function in CSS: Rotating HTML Elements
Rotate Function in CSS: Rotating HTML Elements
  1. Include all your HTML pages in your main 'index.html'.
  2. Add an event listener for the hashchange event. In this event, show/hide relevant content based on the hash part of the URL.

Improving Routing with HTML5 History API

an image of a website page with the text'html 5 page for seo '
an image of a website page with the text'html 5 page for seo '
Frontend Roadmap for Beginners 🌐✨
Frontend Roadmap for Beginners 🌐✨
Express.js Routing Explained Visually — How URL Paths Hit Your Routes
Express.js Routing Explained Visually — How URL Paths Hit Your Routes
an image of a computer screen with the words body and name
an image of a computer screen with the words body and name
the page structure in html5
the page structure in html5
an image of a web page with the words'body'and'reader '
an image of a web page with the words'body'and'reader '
Come realizzare una pagina in Html5: novità e semantica
Come realizzare una pagina in Html5: novità e semantica
HTML CSS Layout Roadmap
HTML CSS Layout Roadmap
an image of a web page with the text'html inputt types '
an image of a web page with the text'html inputt types '
an image of a computer screen with many lines on the side and one line in the middle
an image of a computer screen with many lines on the side and one line in the middle
an image of a computer screen with many lines on the bottom and one line in the middle
an image of a computer screen with many lines on the bottom and one line in the middle
an image of a computer screen with the names of different types of web pages on it
an image of a computer screen with the names of different types of web pages on it
Frontend Developer Roadmap - From Beginner to Pro
Frontend Developer Roadmap - From Beginner to Pro
🚀 Web Development Roadmap 2026: From Beginner to Advanced
🚀 Web Development Roadmap 2026: From Beginner to Advanced
the structure of url is shown in different colors and sizes, including red, green,
the structure of url is shown in different colors and sizes, including red, green,
HTML Basic Tables
HTML Basic Tables
Structure of URL
Structure of URL
the frontend roadmap is shown in this screenshote image, with several different
the frontend roadmap is shown in this screenshote image, with several different
How Express.js Routing Works (Beginner to Advanced Guide)
How Express.js Routing Works (Beginner to Advanced Guide)
Web Application, 10 Things
Web Application, 10 Things

While hash-based routing works, it's not pretty (nor 'pretty URLs'). HTML5 History API allows for cleaner URLs. This API lets us manipulate the browser history stack, enabling us to change the URL without reloads.

Forward and Back Buttons

One caveat is that back/forward buttons won't work as expected because the URL doesn't represent a real state. To fix this, we should push a state and pass relevant data to make the back/forward buttons work correctly.

Using pushState

HTML5 History API's pushState method allows us to add new entries to the browser history stack. This method takes three arguments: state (optional), title (ignored), and URL.

Remember, browser support might be an issue with this approach. Internet Explorer doesn't support this API. Always check your target browsers before diving into this method.

HTML routing without a framework can be a challenging but rewarding experience. It expands your understanding of how websites work under the hood. So, go ahead, experiment, and happy coding! And when you're ready for more complexity, you'll appreciate the structure that frameworks provide.

Related Articles

Thenewboston Css Learn Html W3c Responsive Html Css Tutorial Tutorialspoint Review Is Html Easy To Learn Html Css Tutoring Html Neocities Tutorial Convert Html To Zim Best Html Tutorial Reddit Is Tutorialspoint Good