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

The Script Tag

Before we start writing JavaScript we need to know where in the HTML code we will place the JavaScript code. There are three places where we can insert/write our JavaScript:

  1. The head section can accommodate JavaScript as the head section of the HTML file loads before the body section. If you have a requirement where you need to run the JavaScript before the body loads, this is the only place for you: simply open and close the script tags <script> </script> and write whatever JavaScript code needed between these tags.

  2. Anywhere in the body JavaScript can be used, with the same script tags as above.

  3. A separate file. Make a new text file and save it with whatever name you want, as long as the extension of the file is “.js.” You'll write all your Javascript code in this file.You will have two files now: one HTML and one .JS (JavaScript.). Now you must return to the head section of your HTML file and put in a HTML script tag that permits us link to other files and use their content. This is the same old <script > </script>, but to tell the tag which file to link to, we include a parameter called source “src.” followed by the file name. And here it is: <script src=”home.js”></script>. We are telling the script tag that our JavaScript code is placed in a file named "home.js" (which must be located in the same folder as the HTML file, in this example) so link to that file and make the code available in the HTML file. When it loads the script tag's source parameter, it'll fetch the JavaScript code..

What's recommended?

Which way should you go? The third way is highly recommended by the coding community. First of all it will keep all your JavaScript code in one file, making it easy to manage and change. Secondly it will not increase the HTML file code length, keeping it short and clean. Most importantly, the code will be reusable. For example, if you have two files which require the same JavaScript code, these two files can now share the same JavaScript file: and if you need to make a change in the JavaScript code, you only have to change it in that one file and it will be reflected everywhere.

Video Transcript

00:00So first step first is to see where do we put our code?
00:03Where do we put our JavaScript inside of our HTML?
00:06So we have here this long and beautiful HTML
00:09that has also CSS files.
00:11But we don't have any JavaScript inside.
00:14Now, there are three different places
00:15that you could really put your JavaScript.
00:17The first one is inside of the head.
00:19And now, because we're working, as we said, in the HTML5 world,
00:22we're just going to type the way
00:24that you would do it in HTML5.
00:26Now, we would work also on older browsers.
00:28And this is how it works.
00:30You just write script,
00:33and you close that script tag as well.
00:36So you open and close the script tag
00:37just like you would do in any tag in HTML.
00:39Now, in earlier versions of HTML,
00:42you had to define what type of script it is.
00:44HTML5, we don't need to do that anymore
00:46because any script that exists,
00:48we're assuming it's JavaScript, JavaScript 1,
00:50all browsers are doing JavaScript today.
00:53So we don't need to do those differentiations
00:55between the different styles.
00:57As soon as we put script, we know it's HTML5.
01:00There's no need to put anything else but that.
01:02And there we go.
01:02We have our script tag.
01:04And here we could put our JavaScript, all right?
01:06And we're inside of our head now.
01:08So by the way, this is our head,
01:09which basically means that this is going to be
01:11one of the first things that load.
01:12It's going to be loaded before the body is loaded.
01:15Contrary to that, if we wanted to,
01:17we could have created a script inside of our body
01:20anywhere we wanted.
01:21In our case, we wanted to do it in the header
01:23because we wanted to make sure that our script could start
01:25even before our page is loaded completely.
01:28All right, now, if we wanted to put our script
01:30in anywhere else, we could have just gone
01:32anywhere in our body.
01:34Let's say, for example, we want to put it on our,
01:36right in the end just before our body ends.
01:39And we could put there basically script
01:42and close the script tag as well.
01:44And there we go.
01:45And we could have put our script right here, same deal.
01:48And obviously, the only difference are
01:49that this is inside of our body
01:50while this is inside of our header
01:53all the way in the top here.
01:55All right, so we saw our two main ways
01:56of putting JavaScript inside of a page.
01:58Now, the third one, which is probably the one
02:00that you'd want to do most often
02:02and the one that is encouraged by the industry,
02:06is to put your JavaScript in external files.
02:08Why would you want to put your JavaScript
02:10in an external file?
02:11It makes it easier for controlling versions.
02:14It makes it easier also for sharing
02:16between different HTML files.
02:18So if you have the same script, why put it in one page?
02:20You could share it among the pages.
02:22And it also makes you more organized
02:24because then you start thinking about
02:25how do I create it in a way that I could share
02:28between the pages?
02:29So let's see how you could do that
02:30in how we add a script that is with a source.
02:37Script.
02:40And we just put here the word source, SRC.
02:43And we could close our script tag.
02:45That's all we needed.
02:46And we could start putting inside of our script
02:48right away our script, wherever it is.
02:52So in our scenario, we don't have one yet,
02:53so we could maybe create a new file.
02:55So I'll create a new file
02:56and we'll put here a sample right now
03:00and it's gonna look a little bit weird.
03:02Alert.
03:05Hello.
03:07World.
03:08All right, so we're gonna save that file.
03:10All right, so I'm gonna go inside of my JS folder
03:12and I'm gonna save it inside of my JS folder.
03:15And I'm gonna call this home.js.
03:19Gonna save that file.
03:20And now we have our file home.js,
03:22which is inside of the JS folder.
03:24So all I have to do here is type js slash home.js.
03:29And if I save my file now and we look again
03:31at the code that I put in here,
03:32I'm basically calling an alert message box
03:35and sending it the copy, hello world.
03:37So if I refresh my screen now,
03:38we'll see that we're getting this alert box.
03:41It's saying hello world.
03:42And then our application begins only after that.
03:45All right, so there we go.
03:47We learned quite a lot of stuff.
03:48We've covered so far.
03:49We've seen what editors we wanna use.
03:52We've looked at the HTML5 way of putting it in JavaScript.
03:56And we've seen our first sample of code
03:58where we basically created an alert box
04:00and send it some information.
04:02In our scenario, we double quoted it around
04:05and we said hello world.
04:07So we're gonna see more into that
04:08and why we did that soon.
04:11So for now, let's forget about that external file
04:14and we're gonna use our HTML comments
04:16to comment out that external JavaScript file.
04:19So now that JavaScript file is not going to be loaded.
04:22And we're gonna work inside of our script for a bit.

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!