MrHize
MrHize

Reputation: 220

How to navigate url using pushState

i am learning on how to use pushState to navigate url to the exact placement. When click on the button, url changes to 'upload' but does not navigate to the url page. This is what i tried;

home.html

<button onclick="loadUpload()">Upload Page</button>
<div id="upload"></div>

<script>
function loadUpload(){
  history.pushState({}, "", "/upload/");
  document.getElementById("upload");
}
</script>

Upvotes: 0

Views: 1206

Answers (1)

Quentin
Quentin

Reputation: 943537

The entire point of pushState is that it doesn't trigger navigation. It is a mechanism for saying "I am changing the state of the DOM using JavaScript, the resulting state is the same as you would get if you visited this URL" (while making the back and forward navigation buttons built into the browser work).

If you want to navigate with JavaScript then assign a new value to location.href.

location.href = "/upload/"

However, since your JavaScript doesn't do anything except navigate, you should just be using a regular link for this in the first place:

<a href="/upload/">Upload Page</a>

Upvotes: 1

Related Questions