1

I Have 3 Questions URL Related and looking for an answer or source / topics to learn it from:

  1. How to remove the .html extension from the URL
    example: https://stackoverflow.com/questions instead of https://stackoverflow.com/questions.html
  2. How to have Nested Pages in the URL
    example: qyz.com/movies/latest/...
  3. how do i configure these variables in a url? (-, ?v='id', &)
    example: on a movie app where the user can filter the movies the the filters gets stacked and shows the matching results qyz.com/movies/2022-english-action

Note: I'm only familiar with HTML & Vanilla JavaScript

6
  • This is what a webserver should do. What serverside programming language do you (want to) use? Commented Aug 30, 2022 at 1:51
  • 1
    It's hard to implement using vanilla js. try using reactjs. reactrouter.com/en/v6.3.0/getting-started/tutorial Commented Aug 30, 2022 at 1:52
  • @Bergi since I already know JavaScript I would pick NodeJS route to learn this. Commented Aug 30, 2022 at 2:07
  • 2
    You can follow this implementation github.com/praveen-me/simple-vanila-router Commented Aug 30, 2022 at 2:20
  • 1
    "how do i configure these variables in a url?" - you use the URL API (here for nodejs) to manipulate URL strings. Commented Aug 30, 2022 at 2:41

1 Answer 1

2

What you are trying to implement is a Single Page Application (SPA). There is not a correct answer to your question, but multiple frameworks that I could suggest to answer your question - I will tell one example first, and give you a hint of how they work (could differ from one to the other).

One of the simplest that I know is Barba.js, where you put your HTML content in multiple content tags, configure these as pages and their routes, and configure transitions.

Below is an example copied from their documentation (link to this exact documentation by clicking the above hyperlink):

<body data-barba="wrapper">
  <!-- put here content that will not change
  between your pages, like <header> or <nav> -->

  <main data-barba="container" data-barba-namespace="home">
    <!-- put here the content you wish to change
    between your pages, like your main content <h1> or <p> -->
  </main>

  <!-- put here content that will not change
  between your pages, like <footer> -->
</body>

and the router definition in your main-script.js that you import when executing HTML:

import barba from '@barba/core';
import barbaRouter from '@barba/router';

// define your routes
const myRoutes = [{
  path: '/index',
  name: 'home'
}, {
  path: '/product/:id',
  name: 'item'
}];

// tell Barba to use the router with your routes
barba.use(barbaRouter, {
  routes: myRoutes
});

// init Barba
barba.init();

Now back to your question topics, which are important configurations required for any SPA:

  1. -> All requests must be proxied to this /index.html file - if you request for /any/other/path, it should actually serve the /index.html file - see below basic NGINX configuration for reference:
server {
  listen 80;

  root   /usr/share/nginx/html;
  index index.html;

  location / { # Anything that goes beyond path /*
    try_files $uri $uri/ /index.html; # it tries to open files, such as image resources, .js, etc. - if not found, it serves the /index.html
  }
}
  1. and 3. -> This is what frameworks try to simplify. They use methods to explode URL and get query parameters from URL - abstracting all the rules to display/hide content based on the URL you are. Below some snippets of these functions mentioned above.

Explode URL (source):

var url = 'http://www.mymainsite.com/somepath/path2/path3/path4';

var pathname = new URL(url).pathname;

console.log(pathname);

Parse query-params (source):

const queryString = "?product=shirt&color=blue&newuser&size=m"; // This can be fetched from "window.location.search" in real world.
const urlParams = new URLSearchParams(queryString);

const product = urlParams.get('product')
console.log(product);

const color = urlParams.get('color')
console.log(color);

Sign up to request clarification or add additional context in comments.

3 Comments

"What you are trying to implement is a Single Page Application (SPA)" - no, there was no mention of that in the question. OP might do fine with just nginx rules serving some static pages.
Per the title: "How to pass data from URL to JavaScript [...]" - as said, this behavior is something PWA does. It could be achieved with a web server? Yes, but my answer is specific to how it is done through Javascript.
@DantonHeuer your answer was very helpful thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.