Natanael Natanael - 1 year ago 76
Javascript Question

How to deal with a friendly url with parameters in a static webpage?

I have the following URL https://mywebsite/somepage/1234/5678 where somepage is a route to a page and the numbers are are parameters.

My page is a static page, just html and javascript. Ex.: https://mywebsite/somepage.html.

How could I open this page given the above url get the parameters inside the page?

The reason for this is that I have a mobile deeplink direct the user to a website so that it can download the app in case it isn't installed or to the app itself. I don't have the choice to use a dinamic page with a routing system like in Cake PHP or in Spring Framework.

Answer Source

If you want to load the normal page (https://mywebsite/somepage.html) then you could use hashes instead. They don't get seen by the server, so it would serve the regular html file.

// get the url
var url = window.location.href; // = "https://mywebsite/somepage.html#1234/5678"

// get the bit you want
var params = url.substring(32); // = "1234/5678"
//            ^ the length of "https://mywebsite/somepage.html#"

// split it apart into individual parts
params = params.split("/"); // = ["1234", "5678"]

// then do whatever you like with the params
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download