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

Your Script Location Matters

HTML by default loads elements one after each other. That means the location of your script tag had a big impact. By the end of this lecture you will have a solid foundation and understanding of where you should place script tags (and why).

Video Transcript

00:00In our last lecture we triggered the first function. We've created our first JavaScript command ever. So congratulations for that.
00:09We talked about how it's important to know when to trigger your code because code is something that is dependent on things.
00:17So let's jump right in and let's see what that actually means.
00:21So initially we put our script here in our head area.
00:27And when we did that we saw how our page didn't really load or it wasn't accessible until we actually clicked on OK.
00:36Now we really want to have our page loaded up before we show this output panel. And really to do that it's very simple.
00:43All I have to do is grab this code and I'm going to cut it.
00:47And I could just go ahead all the way to the bottom of my page before I close my body and I could literally just paste my script all the way in the bottom here.
00:56And if I do that and I click on save and I go back into my browser look at the difference this time around when I click on refresh the page will load behind and only then our alert will pop up.
01:09Now this becomes very important and that's why I'm showing you this because when we're trying to refer to elements on our stage we need to make sure that they're actually loaded first.
01:20And let me show you what that actually means.
01:23So again I'm going to take my script tag and I'm going to push it right back to the top where it was before.
01:29But this time around what I'm going to do is I'm going to actually go ahead and try to call an element.
01:35Now to do that what I'm going to do is I'm going to delete this. I'm going to comment out this hello chapter one by putting up slash slash twice you see how it's dimmed out.
01:46And instead of that I'm going to create a new alert and in this alert this time around I'm going to call another function.
01:53Let's show you the function first and this function is called get element get element by ID.
02:04Now contrary to our alert which was a global function that wasn't connected to anything when I'm saying get element by ID.
02:15What I really want is I want to get my element one of my elements that has an ID such as this div layer that has a name and has an ID of middle.
02:24So for me to approach this what I really want to do is I want to go to my document which in JavaScript the way I refer to the whole document is using the copy document.
02:37You can see how it gets highlighted in this editor and then I type type dot and when I type in document dot I'm saying I want to do something with this document.
02:47What do I want to do. I want to get get element by ID. What element do I want to get. I want to get the middle.
02:57So again notice how it's a very similar context as we had earlier only contrary to the alert that was floating.
03:04It wasn't connected to something. This next function I'm actually calling and this function belongs to something else.
03:13Now in programming we call this an object. Now this function when a function belongs to an object we call it a method.
03:20Now these names aren't very important and we'll definitely rehearse them and talk about them as we go along.
03:26But literally all this means is that I want to get an element that belongs to the document.
03:32So I'm going to my document and I'm asking from it for an action. I'm asking it to find for me the element that has the idea of middle.
03:41Now I want to be able to see it. So I'm going to go ahead and I'm going to type your alert and I'm going to basically not all read alert.
03:51And I'm going to wrap this function that I got and wrap it around into this bigger function.
03:59So again I have there an alert and I'm wrapping it around this request.
04:04Now what's actually happening is when I'm asking for my element by ID when I'm asking for my document to get me the element by ID it returns to me that element.
04:14So now this div layer will come back to me as an object.
04:18Now once it comes back to me I'm going to send it to my alert because I want to trace it out.
04:23I want it to be visible on that alert box that we've seen earlier.
04:27Now if I click on save and I go into my browser will notice a problem that problem that we talked about earlier where it's not actually going to find it because the page is not loaded.
04:38So it's returning the value of null which means basically nothing. It means nullified or nothing there.
04:45So we need to figure out a way to get it to work.
04:48And we've seen already before that if we would take our script and put it only after actually that area that element was loaded.
04:56So this is after that element has been loaded in.
04:59And if I run my script again once it's actually on the bottom of my screen and I click on refresh again we're going to see that I'm going to get that element returning back.
05:09So again let me just walk you through this again.
05:11So last lecture we've seen how to call a function and that function in this case was one of the default functions that exist in JavaScript that enable us to send an alert out.
05:24The next function function that we learned on in this lecture is functions that belong to a certain object.
05:30In this case we want to approach the document which is an object that exists in JavaScript that's always there.
05:37And I'm basically saying to my document hey could you give me one of your elements that has an idea of middle.
05:44Now if you recall from HTML and CSS there could only be one element.
05:50The rules of HTML is you could only have one element that is called middle in its ID so we can't have another div with a name of ID.
06:00So literally what happened here is we know we're only getting that one item and then we're sending it over to another function so we've done quite a lot of stuff.
06:08This is by the way loosely called is called nesting in a sense because we're nesting one function into another function.
06:16So there we go and that's it for this lecture.
06:20And in the next lecture we're going to talk about how do we keep our script up here or even in a separate page and how do we get it to only load once the document is completely loaded.
06:32And we're going to look at that and see how do we solve that problem in the next lecture.

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!