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

HTML5 onload

In the last video we left off when our video just wasn't ready to interact with. In this video we will take advantage of the onload callback to only start adding our button controls once our document completes loading.

Video Transcript

00:00Alright, so what we want to do next is we want to make sure that we get the file to
00:04first load in before we start interacting with it.
00:07So what we want to do is before we even call this video layer and define its value, I'm
00:12just going to create a global variable called vid external to a function that we're going
00:16to be using now, a callback function.
00:19Now this callback function belongs to the window and it actually is called onLoad.
00:23Now notice that I'm writing everything in a lower case and I'm approaching the window
00:27and not any other HTML DOM element.
00:31I'm not approaching the document because the window itself has an onLoad callback.
00:36Now this onLoad callback will be called as soon as the document completes loading.
00:41And when the document completes loading and only then do I really want to start activating
00:45the next few steps.
00:47And in this next few steps, basically what I want to do is define the value inside of
00:51the video.
00:52Now again, although this is not the basics of JavaScript course and I do encourage you
00:56to take our JavaScript course to get a better understanding of coding in general, our video
01:01object basically, we created a variable that its scope is global.
01:06And the reason to that is because we want to create a few different functions that all
01:10call back to this video object.
01:12So we don't need to get the element by ID each time.
01:15Now we had to wait until the document was ready before we set the value inside of that
01:20video.
01:21Once we've set the value inside of that video, now we're ready to do that console
01:25log to just check and see that the video really is coming back.
01:28So now we're ready to go into Safari again, click on refresh.
01:32And this time around, we're going to see that we have a parse error in line 13.
01:39So I'm just going to go quickly into line 13 and see what did I type incorrectly.
01:44So for one I'll see that automatically the function when I opened it, the editor that
01:48I'm using right now automatically close the brackets as well.
01:51So I'm going to delete that extra bracket, save my application.
01:55Go back into Safari one more time and click on refresh.
01:58And this time around, I'm not going to get an error.
02:01Now not only that, once the load is complete, we'll see that the video object will return
02:06to us once it's ready for us to interact with it.
02:11All right, so that's it.
02:12So we got our first phase of turning our video into something interactive by getting a reference
02:17to it.
02:18Our next step now that we got a reference to it is to actually add the action inside
02:22of our button to activate the action and enable us to actually play and pause our video.

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!