Getting to know the HTML5 Location API
Welcome back to our HTML5 API training. In this section of our training we will learn about the location and the history API's. By the end of this course you will know how to create a smart browser history dynamically controlled via JavaScript.
By the end of this video you will know everything you need to know about the new HTML5 Location API. In other words you will know how to redirect the user to a new page, change the current page and even reload the current page.
Video Transcript
00:00Welcome back. So in this session, we're going to be talking about the location API. And by the end of this video, you'll know everything you need to know about how to work with the location API. So let's get started. So I'm going to jump right into my code. But before that, I'm going to update my HTML file. And deliberately, I'm doing it now in front of you. The name of the file is 004.location.html, which you could find at zero to geek.com. Alright, so let's go right into our application. Don't worry about the errors, because I was
00:30doing a few experiments, and we'll get started. Alright, so as we get started, the first thing that I want to do is I'm going to get rid of the paragraph. And instead of the paragraph, what I really want to do is I want to put here like about three buttons. So I'm just going to go right ahead and create three buttons. And each button will have a different action. So the type is going to be a button. We're going to give each an ID as well. This will be btn one. The first button is going to show us how do we assign URL.
01:00Oh, button.
01:03And the next one is going to be showing us how do we replace a URL.
01:10Last but not least is going to be a button that shows us how do we reload our page? All right. So let's get started. So first of all, oh, let me just make sure that our ideas one, two and three. And that's all we really needed. Now it's time for us to go into our JavaScript. And in the JavaScript, I'm going to go ahead and create a ready
01:30in line event listener. And we'll see what that means, because until now we worked only with not in lines. But let's see what it means. So first of all, document, document dot get element by ID, same, same old btn one. Now I'm going to immediately assign to it an event. So I'm going to go right ahead and do an add event listener.
01:53And I'm going to call the event of click. Now, contrary to it, usually I would put your name of a function such as on click, I'm going to create an anonymous function by just assigning a function directly into the event itself. Alright, so if you did our JavaScript basics course, then you should be familiar with it. If not, you have just been introduced to it. So now in our anonymous function, I'm going to do our location change. It is incredibly easy and really fun. So let's see how it works. Basically, what I want to do is I'm going to basically go
02:23into my window and to location, the location object, which is automatically part inside of my window. And then I'm going to click onto the assign method and say to it, where do I want it to go? And what I wanted to do is I want to assign to our window a new URL and that new URL will let be let it be zero geek calm, maybe even go all the way to our catalog. Alright, so it's going to take user you guys directly into our catalog. Once I click that, and we look into our
02:53again, we have our button, assign URL, button number one, as soon as it's clicked, it should then assign this value and basically redirect our browser directly into that URL. So let's see if it works. So we go back, if we go back into our browser, we'll click on refresh, we should find your three buttons. Now, two of them are not active yet. One of them is called assign. And once we click on the assign button, we'll see that it will automatically go into zero to geek.com right into our catalog. Now, if we click on the back button,
03:23it's going to take us right back to where we were, right? That makes sense. But not anymore. Once we start using the replace URL. So let's see how the replace URL works. And literally what the replace URL does that the assign doesn't do. And again, I'm just going to update the button to be button number two, and everything will remain the same. But instead of assign, I'm going to set replace. And again, replace what it does instead of assign is basically removes our URL from the history and basically replaces it with this URL. And then
03:53in other words, when we go into our page, and we reload our page, but this time around, we'll click on the replace instead of the sign, we'll see that everything will still work, it will still load our application. But if we click on the back button to try to go back, we'll see that it's going to skip that page because that page as if never really happened. And we're going to go right back to our previous example, which was the stage the storage event. So let's go back into our file and again into location. So we could complete this location example by creating one
04:23more controller. And that is the reload. And the reload enables us to refresh our page one more time dynamically using JavaScript. So again, enter application, this one is the easiest ones, I'm just going to copy the event again. And I'm just going to set one last event for button number three. And button button number three will have just a reload. And the reload is again, part of that location API. And there you go, we've just discovered and learn how to work with the three different APIs.
04:53Obviously, for reload, we don't need to put the URL again. And if we click on refresh and click on that reload, this reload button will now refresh our page each time we click on reload. So that's it. That's all you had to know about the location and the location API, the location object is inside of the window, you just type window dot location and then approach one of the three methods, assign, replace or reload.
