The Script Tag
You deserve a congratulations for reaching session 9 of our HTML training mega course. In this session we will take our first dive into JavaScript . We will learn where to put our code and how to know if the user doesn't have javascript enabled on there browser. We will put comments in our code. Just as much as there where 3 ways to put our CSS in HTML there are 3 ways as well for JavaScript we will see how to do them all. We will see how to create interactive images and more. So, let's get started.
Video Transcript
00:00Alright, in our last session we learned about CSS and in this session we're going to learn
00:04about code.
00:06So we learned about style, we learned about the structure of HTML, and the next step is
00:10to learn about how to program, we're not going to learn programming, we got completely
00:14new separate titles for that, but you'll see the basics of how do you integrate code into
00:18an HTML document.
00:20So let's get started right away.
00:22So first of all I'm going to go into my new code and I really just stripped out a very
00:26basic HTML template from an earlier example, if you don't have our source files you're
00:32more than welcome to come to zero2geek.com and download our source files.
00:36Alright, so what I want to do next is I want to add the script tag, and it's very simple,
00:41I just write the word script.
00:43Now in the world of HTML5 this type equals text, JavaScript is just not needed, so you
00:49could delete it.
00:50The reason why it's not needed is because JavaScript 1 is the only scripting language
00:55available and that's the only programming language that you could use in a browser on
00:59HTML.
01:00So the actual definition of what language you're writing is not needed, so you don't
01:05have to write a type.
01:06The same way as we said with CSS that there's only CSS now, so we don't have to define what
01:10type of style it is, same is true for scripting.
01:14So once we created the script tag we're going to put here the most simple example,
01:18the most simple code that exists, and that is the alert function.
01:22Don't worry about what a function is, but when you write the word alert and you open
01:26a round bracket and close a round bracket, and you send it to something like hello javascript.
01:37So basically by setting it a hello javascript, I'm going to put a semicolon here, this is
01:41the structure of the most simple code, and basically what it does is it calls the
01:44function alert, which a function is something that enables the computer to create some
01:48sort of an operation.
01:50If you join our javascript course you'll know everything about it, and we send the
01:54information, we send a string or the value of hello javascript into that alert message.
02:01What it would do is open up an alert message to the user such as alert, and the value inside
02:06of it would be hello javascript.
02:09So I'm going to save that, go into my browser, click on refresh, and once we click
02:13on refresh you see we can have some sort of an alert, and the alert is telling us
02:17hello javascript.
02:18Alright, so it's very simple, obviously not very functional, but to be able to create
02:22functional applications with javascript you're going to have to take one of our
02:25courses, or a book, or any other medium that you would find comfortable learning
02:29javascript within it if you want to learn how to create some code.
02:32We will do a simple coding example later on just to show you how things work,
02:38but for now we're just going to keep it as a simple example of that, and we're
02:41going to move on to the next topic.
02:45Alright, so in our last video we talked about creating a script tag, and we said
02:50that we just write open up a script and then close the script and put our
02:53javascript. Now we automatically put it in our head, but I wanted to say that
02:57it's not mandatory. You could put it in the head area, but if you do put it in
03:02the head area, and you try to access something inside of your page, you might
03:06not gain access to it. The reason is because all of this stuff didn't load
03:10you. Now the way that you solve that is you could take your script and put
03:14it anywhere you want in your application, and it could be even on the
03:20bottom right before your body ends, and if you do that then you'll have access
03:23to all the information. You know what, I want to show it to you in a real
03:27example, so I'm going to show you how do you approach something. So for example,
03:30let's say we want to approach the h1 and get to it somehow. To do that I
03:34would write document, oh you know what, let me show it to you a better way.
03:41So in our h1 I'm going to put here an id, and I'm going to write inside of my
03:44id a value, giving my h1 some sort of name so we could call it by name. So
03:49I'm going to call it my h1. Now that I have a my h1, I could go and
03:55actually call a method which is called document, document. Now again don't worry
04:04too much about if you don't know what a document is, it's basically the document
04:07is basically the HTML document or reference to the document itself
04:12through code. Once we have access to our document I could ask to it to get element
04:18by id, which is a function that would return to me the element based on the id.
04:23And now I could send into it my h1, and again I'm not expecting you to know how
04:28to do these stuff for no JavaScript at this stage, but to get the basic idea
04:32that you're approaching your HTML document and basically asking from it to
04:36get the element based on its id, and we're giving it its id, which is the
04:40exact same id. So we should be getting h1 tag returned to us, but if I do hear an
04:46alert which basically sends the information that I'm getting right back
04:49into the console so we could see it, you'll notice obviously this is our code,
04:54this is the element that we got back, we should get that h1, and that h1
04:58should be sent to our alert box. So if we save that and we go into our
05:02application again and click on refresh, we'll see that we're getting null,
05:07which null in programming means nothing or undefined or we can't reach anything.
05:12Now why do you think we got here nothing? Even though we do have that element,
05:17only that element is in a later area of the page, you have any idea?
05:21Although we do have it in line 11, but it's coming back as undefined.
05:26Now to make this question a little bit easier, if I take the script tag and I
05:29cut it out of line of the header and I put it right underneath the h1 tag,
05:33look what happens. If I save that, go back into my browser and click on
05:38refresh this time around, you'll see that this time around I'm getting
05:41something back, I'm getting that HTML heading element.
05:44Right, because you put it in the body and not the head.
05:47And why would that matter, do you think?
05:49You have it in the head and sometimes you can't bring it out on the page
05:59in the body.
06:00Well basically, I didn't really understand what you said, but basically,
06:04when you're loading an application, it's actually loading it line by line.
06:08And when it reaches this line where the script, if I bring it back up here for a
06:12second, and it reaches this line, it only got to this line.
06:16Now it's trying to find my h1, but at this line, everything that loaded so
06:20far, it just can't find it, it doesn't exist yet.
06:22So when it comes back as undefined, but on the other hand,
06:26whenever my script is in line 10, which is below, whoopsie,
06:31which is below our h1 tag that has that ID, when we get here, all of this was
06:36already loaded.
06:38And because all this was already loaded, we have access to that my h1.
06:43Does that make sense?
06:44I believe so, I believe so.
06:49I don't think you understood me.
06:51What?
06:52Yeah, I did, I got too much, it said, up above it didn't yet load.
06:55Right.
06:56I brought it down to 11, then it was after the h1, and it was able to find it.
07:04Bingo.
07:05Alright, excellent.
07:06So you got it.
07:07So that's basically something that you need to be aware of as you start programming in
07:10JavaScript is when you try to approach things, you might not get through it.
07:14And the reason is it wasn't loaded yet.
07:16Alright, so that's all I wanted to say about that point.
07:19Now the next thing that I wanted to talk about is some browsers, you know what,
07:23let's keep that to the next video.
