Git Product home page Git Product logo

spa-routing's Introduction

Single Page Application Routing Using Hash or URL

One of the most asked code questions during a front-end interview is "Can you create a single page application with routes, without a framework?" In this tutorial, I show you how to create a custom routing system for your single page application using either the hash or URL method...without a framework.

This tutorial shows you how to build a Single Page App using vanilla JavaScript. I show you how to implement client-side routing both ways (hash or URL) in an easy-to-use format that can be replicated for any project.

View video on YouTube: https://youtu.be/JmSb1VFoP7w

spa-routing's People

Contributors

futuredesigngroup avatar thedevdrawer avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

spa-routing's Issues

Replacing deprecated code

Hi.

My first Issue, so go easy on me.

The js uses deprecated code. It still works, and I'm no dev, but I used Codium to rewrite it replacing the deprecated code.

As I say, not a dev, so lack the confidence or ability to check if it warrants a PR, but it does appear to work.

For your consideration, then...

`const urlPageTitle = "JS Single Page Application Router";

// create document click that watches the nav links only
document.addEventListener("click", (e) => {
const { target } = e;
if (!target.matches("nav a")) {
return;
}
e.preventDefault();
urlRoute(e); // Pass the event object to urlRoute function
});

// create an object that maps the url to the template, title, and description
const urlRoutes = {
404: {
template: "/templates/404.html",
title: "404 | " + urlPageTitle,
description: "Page not found",
},
"/": {
template: "/templates/index.html",
title: "Home | " + urlPageTitle,
description: "This is the home page",
},
"/about": {
template: "/templates/about.html",
title: "About Us | " + urlPageTitle,
description: "This is the about page",
},
"/contact": {
template: "/templates/contact.html",
title: "Contact Us | " + urlPageTitle,
description: "This is the contact page",
},
};

// create a function that watches the url and calls the urlLocationHandler
const urlRoute = (event) => {
event.preventDefault();
window.history.pushState({}, "", event.target.href);
urlLocationHandler();
};

// create a function that handles the url location
const urlLocationHandler = async () => {
let location = window.location.pathname; // get the url path
// if the path length is 0, set it to primary page route
if (location.length == 0) {
location = "/";
}
// get the route object from the urlRoutes object
const route = urlRoutes.hasOwnProperty(location)
? urlRoutes[location]
: urlRoutes["404"];
// get the html from the template
const html = await fetch(route.template).then((response) => response.text());
// set the content of the content div to the html
document.getElementById("content").innerHTML = html;
// set the title of the document to the title of the route
document.title = route.title;
// set the description of the document to the description of the route
document
.querySelector('meta[name="description"]')
.setAttribute("content", route.description);
};

// add an event listener to the window that watches for url changes
window.addEventListener("popstate", urlLocationHandler);
// call the urlLocationHandler function to handle the initial url
window.route = urlRoute;
// call the urlLocationHandler function to handle the initial url
urlLocationHandler();
`

Request: Add MIT licensing

Hello! We'd like to incorporate some testing based on this code into our MIT-licensed project. However, since there's no license attribution on this, we have a small problem. @futuredesigngroup or @thedevdrawer, could you please add a MIT license file to this project? You just need to add a file named LICENSE to the root of the project, with the following content (after filling in the copyright info, of course):

Copyright (c) <year> <copyright holders>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

I'm happy to open a PR to do this if it's helpful...

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.