onLoad Event
AT this stage we know how to create functions and even seen them in action but one thing we didn't talk about was the capability of listening into events. this is a more advanced topic but lucky for us there is a very easy way to piggy back on changes such as in our scenario we want to start coding only after the page has been loaded.
Video Transcript
00:00All right, so at this stage, you should have a basic familiarity with variables.
00:04We already covered the basic variables such as strings, numbers, and booleans.
00:09You should have a more or less understanding of how to work with if statements.
00:13We're going to look at them more deeply as we continue and work in our practice here.
00:18And you should have a basic understanding on functions, how to call them,
00:23but also how to create them.
00:24At this stage, we're stepping into objects, and I wanted to take a step back,
00:28and we're going to learn quite a lot of stuff at the same time.
00:31So before we actually go and start creating objects, we're going to start using objects.
00:37We're going to see that everything in JavaScript is really basically based on objects.
00:41To make this happen, I want to create a canvas, which is an HTML5 drawing element
00:46that will enable us to draw things onto the stage.
00:50So we're going to create a canvas, which is going to be a sort of JavaScript object.
00:54And then in that canvas, we're going to draw and create a couple of stuff
00:57and our goal is going to create a small little Pacman.
01:00So let's get started.
01:02All right.
01:02So looking into our code, we're looking into objects.html, which you could find in your source code.
01:08You'll find that the file is completely empty and is just ready for us to start coding.
01:12So let's start coding.
01:13Now, the first thing I want to do before we could actually start using the JavaScript
01:16is two things.
01:18The first is I want to create a canvas element in our HTML itself.
01:21As we're doing HTML5, I'm just going to type your canvas,
01:26and I'm going to give it an ID.
01:27So that ID, we're going to give it an ID of Pacman
01:31because we're going to create a Pacman.
01:33And we're going to set a width and we're going to set a height.
01:35So let's give the width of 550 and let's give it a height of 400.
01:41That's very randomly I decided that.
01:44And we're going to close that canvas as well.
01:46We're going to save the file.
01:47And that's more or less all we're really going to do inside of the HTML itself.
01:51One last thing that we want to do is we want to make sure
01:53that we don't start calling our JavaScript before our HTML file completely loaded.
02:00Now, one way of doing it is just to put our JS calls on the bottom of this screen,
02:05basically right before our body is completed.
02:07But the standard way of doing it is we're going to go into our body
02:11and we're going to add a new element, which is called in programming an event.
02:16So we're going to add an event.
02:17And there's a couple of events that get triggered
02:20whenever something happens on the body itself.
02:22One of them is on load.
02:25So on load will get triggered whenever the page is completed completely loaded.
02:29Now when the page is completely loaded,
02:31we could do a very, very small command in JavaScript.
02:34Now that we know how to create functions,
02:36how about we'll create a function in our objects.js, we'll call it in it.
02:40And then we'll start everything that we want to do inside of that in it function.
02:44So I'm just going to trigger that function by inside of this string area,
02:48I'm going to type in it.
02:49And I'm going to open and close a curly bracket.
02:52Sorry, a round bracket, and then put a semicolon,
02:55making sure that when our on load is completed, we're going to call the in it function.
02:59Now we have to create that function for us to be able to continue onwards.
03:02So I'm going to click into our object.
03:04And let's create that function.
03:05So function in it, open and close route bracket.
03:09And let's just for the sake of testing, I'm going to put here a little alert.
03:14I work.
03:18Click on the refresh button.
03:20Oh, I'm going to do live coding.
03:22And we could see that our script is definitely working.
03:26And we're getting that the live view is all we really needed.
03:31I clicked on the live coding, but we don't need that.
03:33So live view and we'll be able to refresh and get our alerts.
03:36And here we go.
03:37I work.
03:38This is less related to our JavaScript course,
03:40but it's good to know if you're working in JavaScript.
03:42All right.
