5

A stackoverflow question URL includes a servlet, id#, and title like so...

stackoverflow.com/questions/824349/modify-the-url-without-reloading-the-page

My webiste works the same way with URL's like so...

localhost:8443/user/1/admin

The query I do on the backend to get the users info only requires the id number. The name of the user after that is just for show. So if you typed this into the browser for localhost:8443/user/1/a it would give you the exact same page as this localhost:8443/user/1/admin

Stackoverflow is capable of noticing that the end part of the URL is missing and add it back. So if you put this into the address bar

stackoverflow.com/questions/824349/modify

They will change it to this dynamically

stackoverflow.com/questions/824349/modify-the-url-without-reloading-the-page

Now I did reading about changing URL's dynamically on stackoverflow and everyone kept referring to history.pushstate so I tried it. The problem with this is it adds the incorrect URL to the history stack. What I would like to accomplish is change the URL to the path it should be and not include the wrong URL to the history stack. So if the user decides to go back they go back to the actual page they were on last not, stackoverflow.com/questions/824349/modify. Just like stackoverflow does it. How could I do this!?

1

2 Answers 2

2

Check out the replaceState method.

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

Comments

1

Try using history.replaceState instead of history.pushState

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.