02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School
Previous VideoNext Video

Getting to know the HTML5 History API

In the last video we discovered the location API and in this one we will discover how to work with the History API in HTML5. The first methods we will visit in this video: History.back, History.foreword and last but not least History.go method.

Video Transcript

00:00In the last video we discovered the location API and in this one we're going to discover the history API and understand how to work with it.
00:07So let's jump right in. So I'm going to go right into this new example that we have and start customizing it.
00:12I just want to find our buttons that are already set from our last example and just change the names of the methods that we're going to be calling.
00:19The first method we're going to be calling is the back, the history back.
00:24The next method we'll be calling after that will be the history for word.
00:30And last but not least in this group of videos is going to be history.go.
00:36So let's go and see how it works. I'm going to go right into my JavaScript. I already have an event listener for button number one.
00:42So all that's left for me is to actually go to my window, go into the history object.
00:47And again we find in history window.history just like we found the window.location.
00:54So window.history and back. That's all we have to do to create a back functionality.
00:59So let's before we even go and test it out how about we create right away also our forward functionality.
01:05So I'm going to create button number two and button number two instead of calling the back functionality we'll call the forward functionality.
01:15So now we have button one going back, button two going forward.
01:19And if we go into our browser, click on refresh and this time around click on the history.back.
01:25We'll see that we're going to go one step back into our location.
01:28And if I go right back in there and click right away into history forward,
01:32we'll see that we're going to take one step forward if we had anywhere to go, but we don't.
01:36So let me just put here something quick.
01:38Zero to geek.com obviously let it load for a second.
01:43Click on the back button. Now that we went on into our back button,
01:47we can now click onto the history forward and the forward would work.
01:50Notice how no errors are getting generated.
01:52This is literally something that is silent.
01:54They were just basically telling the browser go forward.
01:57If the browser didn't go forward, then no harm done.
01:59Nothing would actually happen.
02:01So let's go to one more thing before we wrap up this video.
02:05So the last item that I want to talk about in this group of buttons or of actions of the history button,
02:11which by the way is not complete contrary to the location that we did the whole location.
02:15We are only in this video only approaching three methods,
02:18which was the back forward and the go.
02:21Now before we even go, I want to show you something.
02:24But if you do your cut, if you click on console console,
02:28so sold out log and you log out your window history length,
02:34because the history object is an array as well.
02:37This array will basically tell you how long or how much information does it have.
02:41And if we go back into our browser and click on refresh,
02:44we will see that our information when we click on our history go,
02:48which doesn't work, it has seven positions.
02:50We know there's one forward and that means we have six backwards.
02:54So how do we actually tell the browser to go to a specific step?
02:58The way we do that is actually I'm going to just comment that out.
03:02The way we tell it to go to a specific step is to saying to the window.history.go.
03:08And instead of we don't say the actual position because you never know what the actual position is,
03:12you actually set a delta, which is basically or a positive value or a negative.
03:17If we put a positive one, it would be the exact same thing as forward.
03:21If we do minus one, it would be the exact same thing as back.
03:24So how about we take two steps back by putting minus two inside of our go function.
03:29Once we do that and we go back into our browser and click on refresh,
03:33once we click on our go back, we should go two steps back.
03:36So if we click on our go back, we're going to get back to example number three,
03:40which is correct.
03:41That was the two steps back.
03:44So there we go.
03:45So we learned how to use the go, the back, and the forward methods
03:49of the window.history API, HTML5 API,
03:53and we also discovered the length property of the history API.
03:57Hope you enjoyed it and goodbye for now.

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!