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

Taking a peak at the XMLHttpRequest

Before we can continue working with the History API of HTML5 we need to take a break so we can take a deeper look at the way we will be loading data onto our site dynamically %u2013 to do that we will load in only portions of the page using the XMLHttpRequest methods.

Video Transcript

00:00Welcome back. So we're not done yet with the history functionality, but we're going to take
00:06a break from history to be able to build up an application of sorts or a functionality of sorts,
00:11and then revisit and go back and continue working on the history API. So one of the stuff,
00:18one of the features of the history API enables us to basically replace only smaller components of
00:24the page. Can't do it alone. To be able to do that, we have to introduce ourselves to
00:30a few different functions and a few different functionalities before we can go back to it.
00:34All right, so let me just show you why an example of why would you want to do that.
00:39So as an example of zero to geek.com currently, and zero to geek.com, we have our video player in
00:44our course, and it has a couple of videos. Now, once you click on to the next video, really,
00:50the only thing that's changing in the video player itself is the actual content in the video area
00:55itself, and actually where you're currently at. But our application itself has to reload the whole
01:02page, creating a little bit of an odd experience to the user. It wouldn't be a much better experience
01:09to the user if when we click on the next button, it would only replace the content that needs to be
01:14replaced, and it wouldn't actually replace the whole page, making the load process much faster.
01:20That's going to be our experience that we're going to try to develop together in this video,
01:25basically. So going back into our examples, I created a simple kind of example where we have
01:30a very basic slideshow. Once you click on to two, it goes to slide number two, and then it would
01:37have slide one and three, and when you click on three, it would go to slide number three,
01:40enabling you to push into one or two, and the image gets updated. Literally, if we go into our
01:47code itself into our slideshows, I just want to show you the area that's important. So we have
01:52our slide container itself, and really the content, the change inside is really just this area,
01:57the content inside of the slide. I've added also an ID to the slide itself, and I've added an ID
02:04to the hrefs that are there. So there's a slide two, slide three here, and in two, we have slide
02:09one and three, and in three, we have slide two and three. Obviously, this is not a live example,
02:13but we just saw an example where it would be really relevant in a live example.
02:19The next thing that we need to do is we need to create some sort of, in theory, this would
02:23probably be done on the server side. But what we're going to be doing is we're going to have
02:27a fragments folder inside of our source files. And if you don't have our source files,
02:31you just have to go on to zero to geek.com. If you're one of our members, you could download
02:34the source files. But if you look into the source files itself here, in these files,
02:40these segments and files literally just have the things that changed in the page,
02:44only the components that basically change in the page. And that's true for all three. And I made
02:48sure that the names of the files were exactly the same as the name of the files here. But it's
02:53just the fragments of basically the things that change. Alright, so now that we got our starting
02:57point, and we know where we are, where we're starting from, it's time for us to start working
03:02on our JavaScript. So I'm just going to open up our JavaScript. And I want to start us off also
03:07from page one. So let's start from slide one. Now that we're in slide one, let's go back into
03:12the JavaScript and start building it out. So we know that we have in our slides, basically,
03:19h refs, basically, let regular links, which will work if the history component doesn't work,
03:23or if we can't do this dynamic functionality. So once now that we next step for us really
03:30to do is to basically build up a bypass functionality for this for the a tag. So to
03:36do that, I'm basically going to just call out a function that will create momentarily,
03:41I'll call it dynamic, dynamic load slide, and we're going to dynamically load slide one,
03:48which basically if it's there, if the buttons there, then we'll load it, if not, we won't.
03:53And the same we'll do for two and three. And this is literally just for the sake of the example.
03:57Alright, so let's create that function. And then dynamic load slide gets an ID. And literally,
04:07the first thing that we want to do is get that h tag. So we're going to go to document dot
04:12get element by ID. And we're going to approach that ID. Really, what we just want to do is
04:19make sure that that a tag exists. And if it doesn't, we're basically done with everything we
04:24wanted to do. If a tag does exist, then it's time for us to add a override functionality.
04:30So I'm going to add a new event listener. And I'm going to now let's do it right inside here,
04:40now we'll call a function, an external function, fun action. Oh, let's call it on click on slide
04:51click. There we go. Right, so that's all we had to do inside of our dynamic slide loader.
04:59For now, our next step is to go to create that function. So let's create that function,
05:04that deals with the scenario where a tag was clicked, where h ref was clicked when a HTML
05:12element has been clicked. So now that we're inside of it, the next step for us is the first thing
05:16and actually, we want to do is we want to prevent the default, preventing the default, we'll make
05:23sure that our JavaScript will not execute the actual trigger anymore. So if we go into our
05:28code itself, into our code, again, by preventing the default, we're basically basically stopping
05:35the function for now, from continuing the default behaviors. So if I go back into my
05:40application and click on refresh, this time around, when I click on to it shouldn't do anything.
05:44Perfect. And that's exactly what we hoped for it to do. All right, so back into our code,
05:49now that we prevented the default from happening, it's time for us to approach the actual link
05:53itself and start modifying it, because we really only want to load a modified variation of that
05:59link. So let me just go into the console for a second. And I'm going to find that link,
06:03which is going to be in my target, and then it's going to be href. And I'm going to look at the
06:09link itself and then modify the link. Because what we want to do is we want to look instead of load,
06:13if we look into our source files into our folder, instead of loading the slide two or three or
06:18whatever it would be, we want to add the folder fragments in between the two. So we just
06:22want to make sure that what to see what we get out of it, when I'm going to click your refresh,
06:26click on slide two. And now we can see the full directory path is which is a local directory
06:31path that goes to our slide two. But literally what we want to do is we want to kind of like
06:35put right here in between another folder. So to do that, I'm just going to approach it in a very
06:40simple way, what I'm going to do is I'm going to split it out, I'm just going to save my as a
06:45variable, I'm going to save it as let's call it a link. And I'm going to take that e dot target
06:51dot href. And I'm going to split it and take out of it the actual slashes. Now that it's split
06:58down based on slashes, and we're it has an array of a link that is positioned based on those slashes,
07:04I could then approach the last element in a link, approach last element in a link and then replace
07:13the content inside of it with the exact same content that it's that's in it right now.
07:19But before that, add that fragments folder. Alright, so now we added that extra folder
07:29inside, all that's left for us to do is to actually convert that right back into
07:34to our link. So basically a link split, we already split, so now it's joined, we're going to join that
07:41array together. And we're going to use the same separator that we use to basically concatenate it
07:47to break it down. So now that way we're concatenating it. So if we go back into our browser
07:52and click on refresh, this time around, when we when we click to see it, we'll see that the
07:56folder fragments is right there in between exactly where we want it to be. The only thing
08:01that's left for us to do right now before we could be done really is to load in this page,
08:06but not override our page, but loaded as an internal component. To do that, we're going to use a new
08:12object that we haven't worked with before, which is called XML HTTP request. Microsoft are the ones
08:18that created it. And since 2006, it's been part of the draft of the W3C, all the browsers
08:24supported and it enables us to load XML documents, which HTML is an XML document.
08:31So let's see how we do it. It's very simple, we're not going to dig into the essence of
08:36the language itself. Let me just add here a lot of enter so we can push our arrays
08:40into the center of the screen. Beautiful. So basically, I'm going to create a new response
08:46object or request object, and I'm going to create a new XML HTTP request object. Once I've created
08:55the object, that's it, I have an object, all that's left for me to do is to open up a new
08:59channel. And I'm going to say to it, what do I want to do? First of all, do I want to post
09:03information or do I want to get information? And in my case, I want to get information,
09:08I don't want to send really any information. There's no, I'm not sending any data with this
09:12URL. The next step is I want to actually get the link itself inside, which we saw,
09:17we created the link that we wanted. So we could move that right into that function,
09:22we could delete that. And the next property, we want to set it to be false. By default,
09:28it will be asynchronous. Or in other words, it will not stop at all operations and wait until
09:33this information returns. In our case, we definitely want to stop all operations. And we
09:38want to wait for the information to first return before we move on to the next step.
09:42So now that we got our information, we know what we want, and we set our link, and it's ready to
09:47to work. All that's left for us to do is to send the operation by calling the send method.
09:52Now some also add the null here. But literally, if we wanted to send data, we would send that
09:57data right here. In our case, we don't have data. And by default, the value will be nothing or no.
10:03Alright, so we got our HTTP request set, our information should be back to us already in line
10:0922. So all that's left for us to do is to check to see if the status of the object that returned
10:15is 200, which basically means that the information came back with any without any issue.
10:20If the information came back without any issue, now it's time for us to basically
10:24bake the results that came back right into our HTML document. And if we recall, we gave our
10:31slide class our basically super slide class, we gave it an ID of slide. So all that's left
10:37for us to do is basically call that slide property. So document dot get element by ID
10:44and get slide. Once we have slide, we want to change its inner HTML. So now to watch
10:50we run a replace it, we want to replace it to the value that comes back from our HTTP
10:55requests, our XML HTTP request, when we will get that through the property response,
11:02text, text, not test, text, response text, there we go. So we got everything we need. And our
11:10information has returned and it's working. customary, what we want to do next is return
11:16true, making sure that we're done with our operation. Now, if our HTTP status did not work,
11:22we'll want to return false, basically making sure that our our prevention of the defaults
11:29would be undone, or you might want to in this scenario may be called the location API. But for
11:36but these type of cases are more rare, and obviously, we're not as worried about them.
11:42So that's it, we've created the document, we've created all the functionalities that we wanted,
11:46we're going to walk through it one more time. But before that, we want to go back into our browser,
11:50click on refresh, and see that it actually works. So when we click on the on slide two,
11:55what we're hoping is that the content itself would load, which it does. But unfortunately,
11:59it's loading to us undefined, which might mean that we have some sort of error in the actual
12:04maybe location of the file that we were trying to load. But let's take a look a deeper look to
12:10try to figure out what the problem is, it could be one of any two issues. One issue that might
12:14be is that we didn't type our response text correctly. Another option which it might which
12:20makes sense because we see that there's no highlighting of the of the answer. So if we
12:24look at this more closely, we might see that we have a typo here. So let's try this again,
12:29response text. And here we go. And now we could see that our copy is changing as we would want
12:37it to change. And we could see that that's where issue was. Now, if we if this wasn't our issue,
12:42the issue probably would have been where we loaded the file, we might have loaded the wrong
12:46file. Alright, so back into into Firefox, and we're going to click on refresh. And this summer,
12:52we're going to try to click on slide two. And we'll see that it automatically loaded in the
12:56inner part without loading the full page all over again. And there we go, that not the whole page
13:06refreshed, only the actual content itself that was updated has been refreshed. And that's it.
13:14In the next video, what we're going to do is we're going to continue on this example.
13:18And actually update our history, because if you can see right now, when we click into slide two,
13:23and if I go back here to slide one, and just click on refresh, because some of these files are
13:27not up to date, because we've been going back and forth, and some of them are older files without
13:32the historical updates. So when we're in one, and we click into slide two, notice how slide two
13:38gets updated, but we never leave slide one. But what we really want is to be able to update our
13:44URL to reflect the fact that now we're in slide two. So if the user clicks on the back button,
13:49the functionality would still work as the user would expect. So in the next video,
13:53we're going to talk about that. And we're going to see how could we solve that issue.

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!