1

I know about setting and getting URL parameters, e.g. example.com/index.html?x=foo&y=bar&z=hello would give your javascript access to the x, y, and z variables specified in the URL.

What I wish for is the same behavior but utilizing the file path part of the URL. E.g. example.com/index.html/foo/bar/hello.

How can I accomplish this? In particular,

  1. How can I make sure the browser knows to go to index.html even though the file path index.html/foo/bar/hello was specified?
  2. How can I update the URL to example.com/index.html/foo/bar/world, such that if I press "Back" on the browser, it will go back to example.com/index.html/foo/bar/hello.

Furthermore, I'd heavily prefer if the solution was in pure javascript/html, without reliance on jquery or any other libraries.

6
  • But /foo/bar/hello. should be a file to access it. Commented Jun 28, 2020 at 1:17
  • 1
    Would probably make more sense if the path after index.html was a url hash value .... example.com/index.html#/foo/bar/hello. Server would only know index.html and your front end could parse the hash to do whatever is needed from location.hash Commented Jun 28, 2020 at 1:19
  • Alternative is you need server side to manage sending the index.html when those other path parts exist. Not known what stack you use on server Commented Jun 28, 2020 at 1:24
  • This is something that can only be done server-side Commented Jun 28, 2020 at 1:28
  • So I've found that History.pushstate can accomplish this with window.history.pushState({x: "foo", y: "bar", z: "hello"}, "", "/foo/bar/hello"), with navigation working fine and everything. The only thing is that directly entering the url example.com/index.html/foo/bar/hello won't work properly. You're saying the only way the URL could work properly is messing with the server side, then? Commented Jun 28, 2020 at 1:38

1 Answer 1

1

There's 2 scenarios you should handle.

  1. What if the user is on a foreign page and wants to visit your page using the link example.com/index.html/foo/bar? This should be handled on the server not on the client side.
  2. If the user is already on some page, say example.com/index.html and wants to go to example.com/index.html/foo/bar, you may use the pushState method. You may look at https://developer.mozilla.org/en-US/docs/Web/API/History/pushState to change the current URL & and this https://developer.mozilla.org/en-US/docs/Web/API/WindowEventHandlers/onpopstate to do something when the user presses the back button.
Sign up to request clarification or add additional context in comments.

Comments

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.