Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

125
Views
Route are not working in vanilla Javascript SPA

I am working on a single page application, now stuck at routing, I have tried different ways but my code always returns 404. I need some help now

const route = (event) => {
  event = event || window.event;
  event.preventDefault();
  window.history.pushState({}, "", event.target.href);
  handleLocation();
};

const routes = {
  404: 404. php ',  // http://localhost/spa/404.php',
  'index': 'index.php',
  'preparing': 'preparing.php',
  'completed': 'completed.php',
};

console.log("routes ", routes);

const handleLocation = async() => {
  const path = window.location.href;
  const route = routes[path] || routes[404];
  console.log("routes", route);
  const html = await fetch(route).then((data) => data.text());
  document.getElementById("main-nav").innerHTML = html;
};

window.onpopstate = handleLocation;
window.route = route;

handleLocation();
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!