Discovering the HTML5 History pushState method
In the last video we toke a break from the History API to learn how to work with the XMLHttpRequest . In our next goal now that we have a working application that will load in data dynamically we would like to be able to control the URL of the user and enable the user to be able to change the page url without refreshing the content on the page.
To do this we will be using the 'pushState' method.
Video Transcript
00:00In our last video we took a break from the history as we were looking at a detailed example
00:05of how to work with the XML HTTP request. Big word, but we saw how to work through it where
00:12we're able to load parts of the page without reloading the whole page. The next step for us
00:18is to make sure that when we click on to different pages that it will update our browser
00:22even if it doesn't do a full refresh. To make that happen what we want to do is we want to go
00:27back into our application and we're going to see how to work with another history component.
00:31It's quite easy. So in our history component first we want to find where we had our successful
00:37change of content which basically happens between line 23 and line 24. What we want to do is we
00:43want to go back to that window.history and we basically want to update the information.
00:49We want to update the information in the browser. To do that we'll call the push
00:54state. Now the push state itself has a first property which accepts some sort of a string
00:59which will have data. We're going to look at what that is in the next video where we'll explore the
01:04events of the history object. But for now we're going to put null in it. In the next step we
01:10could put a title inside. Most of the browsers today don't really have something that really
01:14works for the title. So we're not going to send anything inside of it but in theory you could
01:18send inside of it information for the title of your browsing. So basically it enables you when
01:24the user clicks on the back button to be able to see some information about what is or where was he
01:29instead of just saying the title of the page. That was or nothing for that matter. But we're
01:35going to keep null there and let the browser do whatever it does by default. And the next
01:39thing is we're going to put the URL of the actual page itself. So the URL not the one of
01:44our fragment but the URL of the link itself that we want it to be. And the link itself is that
01:49e.target.atref. So we're going to put that full link right inside there. And that's all we had to
01:55do. By doing that we're basically forcing our browser to update the pages out without actually
02:01refreshing the page. So if we go back into our browser and we click refresh and this time around
02:08we go into slide three we'll see that slide three will be updated as well. And from now on it
02:13will always update where it's at. Both if our load didn't work then it'll load the full page.
02:19And when our load does work it will still update and reflect that information making it possible
02:24to click on the back button and go back and forth inside of the pages. Now the only problem that we
02:30have right now is if the user clicks on the back and forth buttons they're actually going to
02:35really reload the whole page. Lucky for us the history component enables us if we are the
02:41ones that set the push state and if we set a push state we could listen to an event to
02:46listen when those events change. In the next video we're going to look at that.
02:51Now before we go into that topic I just wanted to see a side note. We're talking about the push
02:55state but there is the capability of replace state. And again the replace state will work
03:01very similarly to the way it worked with the location. It basically deletes the current
03:07state and replaces it with another one. While the push state adds a new state which is
03:13moving aside the one that was before it to be the to be the back button
03:16instead of getting rid of it completely. So if you wanted to get rid of it completely
03:20instead of push you would write replace state. See you in the next video when we
03:25start looking into the events.
