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

Using external Files for our JavaScript

Using external Files for our JavaScript is simple and important. Why write up new JavaScript for every single page when you can use the same code over and over. In this session we will explore how to integrate external files into our HTML project.

Video Transcript

00:00Alright, so while you were gone, I created an empty JavaScript file so we could start moving our files out the way you do that
00:07You just create a file with the extension of dot JS for JavaScript
00:11Just as much as we had dot CSS for our CSS styles and dot HTML for our HTML
00:16Alright, so if we go into our editor and we see our empty JavaScript file
00:20All I really want to do is I want to grab my code from that from my script
00:24Obviously, it's a very very lame code right now
00:27Which you could do much better code than that if you are one of our students in our JavaScript course
00:31We now we don't even need our script anymore
00:34We could still keep our no script if we want to but I'm gonna do a fresh clean slate
00:38So I'm just gonna delete everything if I cared about non script browsers
00:41And I might put it there and you could keep it if you wish
00:44Alright, so let's see how we could do this
00:46So first of all, I'm gonna go into my head, but again, I could put it anywhere in my body
00:49I could put my script tag
00:51The next thing I'm gonna write inside is my source just as much as we did with images
00:55All that's left for me to do is to tell it where that image is
00:59external.js
01:01Now it is easy and it will get a little bit harder
01:04So one one is usually you always open and close a script tag. Don't ask me why I don't really get it
01:10Why why you have to do this because I usually always just close it up like this. No one ever complained
01:15But you know just to be consistent with every manual out there you open and you close your script
01:21Even if there's nothing inside of it
01:23Now in my script tag itself
01:25I that's all I had to do once I save this and I save my external JavaScript file
01:30My code itself is now just going to load automatically in this line in line number six and execute
01:36So if I go back into my browser and I click on refresh first of all, we'll see that now we're getting null
01:42Which means that our it's not finding the information, but it is loading the JavaScript file
01:48Now again, the reason why this is happening is because the code is being executed in line six
01:52And it was before the rest of the page was loaded
01:55Now one way for us to solve this problem is by adding a property that's called defer
02:01now the defer property basically tells the browser to not load this did not load this file until it's done loading the
02:09Whole page if you do do that and we go into our application again, and we click on refresh
02:15You'll see that we're going to get our Java's your results back without any problem now because we're working in HTML
02:225 it really doesn't matter that we don't have to build what is called XML
02:28Legal code in XML. You would never write a property without a value because everything is a key value key value
02:35Now if you really want to keep your application
02:38kosher and that to be
02:41Xhtml compliant all you would have to do is make right defer equals
02:46defer
02:47Now obviously that's silly and in my perspective not really needed and because the HTML 5 world thought it's not needed anymore
02:54They agreed that you just don't have to write it now
02:57If you you do want to be HTML 5 if you want to be X HTML compliant
03:02Which isn't really important these days, then you would have you would do that. All right
03:06But that all you have to do is just write defer and it's kind of like a Boolean value
03:10It's a flag that tells the the browser. Okay load this later on and not right now
03:15Another hand there is a property that is called async which basically says
03:20Contrary to traditionally where you when you load something if you don't write anything the application will stop loading
03:27It stops the progress it first loads the file and then it continues loading whatever is underneath it when you say defer
03:34It basically says don't do this now
03:36Imagine that I've put this in the last line of my code and the only load it after everything is done
03:41right now the third option which is async is a little bit different async basically says start loading this and
03:49Execute it immediately, but continue loading everything else as you're going through
03:53Alright, this is for more advanced programmers and it's pricing
03:56You wouldn't want to do yourself the most common thing you probably want to do is or defer
04:01Or taking your script and just literally putting your script tag all the way as the last thing on your page
04:07So you could literally if you wanted to you just put it as your last thing in your body
04:10The most the last place that's legal for you to put script
04:16No, that's good you're starting to get it. All right, so you could have said defer or not. That's your choice
04:22Exactly. All right. So that's really all we have to say about
04:25Loading JavaScript and how do you get JavaScript into your page itself?
04:29And again, if you want to learn JavaScript and learn how to do JavaScript mama, what should they do?
04:34Go to zero to keep
04:36Dot-com or check our profile wherever you are to find if we have an offering of JavaScript there
04:43Thank you

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!