- Overview
- Transcript
- View Offline
-
-
-
-
Before we get much further, let's talk a little bit about where our JavaScript should go. You see creating JavaScript like this, what's referred to as inline code, is okay to get us started but is not how we're going to continue. The problem with having your JavaScript mixed in with your HTML is not an issue on a tiny test page like this, but when you start creating more complex pages your code becomes fragmented. It becomes hard to edit. It becomes hard to maintain. And because you often do the same thing across dozens or hundreds of pages, you've got duplicated code and if you make any changes, you might have to make them dozens or hundreds of times.
So instead of having it this way, what's referred to as inline JavaScript, we're going to put our JavaScript in a separate file like you might do with CSS. It's easier to maintain, it's easier to use across multiple pages, and as you'll see, it's even easier to write this way. We simply cut our JavaScript from the HTML and put it in a separate file. Now we don't need to copy across the script tags; we keep those in the HTML page, because we need them to describe the link between our HTML page and the JavaScript. And we just use the source attribute, the SRC attribute, like you might do with say an image tag.
Now your JavaScript file is just text. It could be any file name, but by convention it ends with.js. Now right now these two files would need to be beside each other in a folder or subfolder, because the SRC, the source attribute, obeys the same rules as linking to an image or a style sheet file. This could be a relative link. It could be an absolute link. It could even be on another server. In this course, I'm going to tend to keep them in the same folder for simplicity. And then we can just squeeze up the script tag, because there is nothing inside it. Now we have a great division between our HTML and our JavaScript.
Let's talk a little bit more about the script tag itself. When reading other people's JavaScript, you're going to very often see this type attribute in your script tag. And this is saying not only this is script, but this is JavaScript. Officially, it could be a whole range of different options, and it's hearkening back to the days when we had to worry about what particular flavor of scripting language we were using. But if you leave it off, every browser will assume it's JavaScript anyway. Now yes, the type attribute is formerly required if you're fully complying to the HTML 4 or XHTML spec, but in the HTML5 spec it's officially optional and just defaults to JavaScript.
Now some code editors will add the type attribute automatically, and some others will complain if you don't have it. So it might be easier for you to leave it, but I have never encountered a single situation while leaving it off has any impact at all. So I'm in the rather large count that says these days just leave it off. And next question, where do you put the script tag? It's very common, as we've said before, to see the script tag in the head and although this will work, I'm not a big fan of this, because remember JavaScript is parsed and interpreted as soon as it's hit by the browser, and that means your JavaScript file could cause a delay in rendering the page.
And we really don't want to slow the page down. We want the browser to load the page, and then we'll have our script come along at the end. Now there might occasionally be a need to immediately load and process some JavaScript. For example, you might be using JavaScript to actually create the page content. Then okay, you want it as soon as possible. But if there is a good rule of thumb, it's to have your script tag not just in your body section, but at the very bottom just before the closing body tag. Now, if I'm linking to style sheets, I'll keep those links in the head section so that they do load immediately, but my general rule is style sheets up at the top, script files down at the bottom.
Now, later we might even have multiple script tags here to link to multiple JavaScript files, and then we could get concerned about what order they are in, but we'll cross that bridge when we get to it.
-
-
-
-
-
-
-
-
-
-
-
-
-
Released
7/22/2011- Understanding the structure of JavaScript code
- Creating variables, functions, and loops
- Writing conditional code
- Sending messages to the console
- Working with different variable types and objects
- Creating and changing DOM objects
- Event handling
- Working with timers
- Debugging JavaScript
- Building smarter forms
- Working with CSS, HTML5, and JavaScript
- Using regular expressions


Share this video
Embed this video
Video: