
Sign up to save your podcasts
Or
Up to this point we haven't talked about the elephant in the room!
And that's the fact that we've been talking about JavaScript for quite a while now, but we haven't yet discussed how to actually USE JavaScript inside of an HTML page.
Well, no time is better than the present right? Let's talk about JavaScript and HTML.
As I just mentioned, you've already seen many examples of JavaScript syntax, but we haven't seen the link between an HTML page and the JavaScript code.
The link between them is pretty straight-forward, you just need to know that there are two ways to link your JavaScript code to your HTML pages:
Now that you know there are two ways to link your JavaScript to your HTML, let's take a look at each approach.
If you happen to have some JavaScript code stored in a file (like we saw in the last article on debugging in JavaScript) then there's a simple way to “import” this JavaScript code into your HTML page.
Let's first assume that we have a basic HTML page like so:
Secondly, let's also assume that we have a JavaScript file that's called “Debugging_Example.js” and we'd like to include it into our HTML page above. In this case, we've already seen this JavaScript file in the last article on debugging, and we know that it is stored in the following URL: https://www.coderscampus.com/wp-content/uploads/2015/07/Debugging_Example.js
Note: If you like, you can even click on it and see the contents of the JS file in a new tab
Okay, so now we know where our example JavaScript file exists, where do we go from here?
Well, in order to import this JavaScript file into our HTML file, we just need to use a
SUPER IMPORTANT NOTE: When using the script tag, it is mandatory that you use the closing tag just as it appears. Don't try to use the “short-cut” approach like this
And voila! You've now successfully imported the “Debugging_Example.js” file into your simple HTML page.
Pretty simple to understand I hope.
There is also a second way to include JavaScript code in your HTML files, and that's by directly embedding the JS code itself.
This method is nearly identical to the one above, and thus the same rules from above apply here. You still need to use the proper closing tag when embedding JavaScript code.
The only difference when embedding JavaScript code is that you don't specify a src property, as you're not importing the code from an external file, so there's no purpose to trying to specify the source of the code right?
Here's what embedding our “debugging example” code would look like:
Boom! You've just learned how to directly embed JavaScript code into your HTML pages.
So you're probably wondering when you should use either of the two options for including JavaScript code on your HTML pages.
And the answer is also pretty simple.
Generally speaking, it's best to separate your JavaScript code from you HTML code.
This means that it's usually a best practice to create separate JavaScript files (just like our “Debugging_Example.js” file) and import them rather than embed them.
When you embed a LOT of JavaScript code, it can get a little bit hard to follow along with the HTML file. So you should try to always favor importing over embedding.
In order for your HTML files to make use of JavaScript code, you'll need to include the JavaScript code IN your HTML files in one of two ways.
Either you import the code from an external JS file, or you embed the JavaScript code right into your HTML page.
Both approaches will get the job done, but you should try to favor importing your code from an external file, as it's best to separate your HTML code from your JavaScript code for easy readability.
Oh! And don't forget, we're always offering SWEET stuff in the email signup form below. So if you haven't already, be sure to check out what freebies we're offering and sign up below.
The post EP33 – Importing JavaScript into HTML via Script tag appeared first on Coders Campus.
Up to this point we haven't talked about the elephant in the room!
And that's the fact that we've been talking about JavaScript for quite a while now, but we haven't yet discussed how to actually USE JavaScript inside of an HTML page.
Well, no time is better than the present right? Let's talk about JavaScript and HTML.
As I just mentioned, you've already seen many examples of JavaScript syntax, but we haven't seen the link between an HTML page and the JavaScript code.
The link between them is pretty straight-forward, you just need to know that there are two ways to link your JavaScript code to your HTML pages:
Now that you know there are two ways to link your JavaScript to your HTML, let's take a look at each approach.
If you happen to have some JavaScript code stored in a file (like we saw in the last article on debugging in JavaScript) then there's a simple way to “import” this JavaScript code into your HTML page.
Let's first assume that we have a basic HTML page like so:
Secondly, let's also assume that we have a JavaScript file that's called “Debugging_Example.js” and we'd like to include it into our HTML page above. In this case, we've already seen this JavaScript file in the last article on debugging, and we know that it is stored in the following URL: https://www.coderscampus.com/wp-content/uploads/2015/07/Debugging_Example.js
Note: If you like, you can even click on it and see the contents of the JS file in a new tab
Okay, so now we know where our example JavaScript file exists, where do we go from here?
Well, in order to import this JavaScript file into our HTML file, we just need to use a
SUPER IMPORTANT NOTE: When using the script tag, it is mandatory that you use the closing tag just as it appears. Don't try to use the “short-cut” approach like this
And voila! You've now successfully imported the “Debugging_Example.js” file into your simple HTML page.
Pretty simple to understand I hope.
There is also a second way to include JavaScript code in your HTML files, and that's by directly embedding the JS code itself.
This method is nearly identical to the one above, and thus the same rules from above apply here. You still need to use the proper closing tag when embedding JavaScript code.
The only difference when embedding JavaScript code is that you don't specify a src property, as you're not importing the code from an external file, so there's no purpose to trying to specify the source of the code right?
Here's what embedding our “debugging example” code would look like:
Boom! You've just learned how to directly embed JavaScript code into your HTML pages.
So you're probably wondering when you should use either of the two options for including JavaScript code on your HTML pages.
And the answer is also pretty simple.
Generally speaking, it's best to separate your JavaScript code from you HTML code.
This means that it's usually a best practice to create separate JavaScript files (just like our “Debugging_Example.js” file) and import them rather than embed them.
When you embed a LOT of JavaScript code, it can get a little bit hard to follow along with the HTML file. So you should try to always favor importing over embedding.
In order for your HTML files to make use of JavaScript code, you'll need to include the JavaScript code IN your HTML files in one of two ways.
Either you import the code from an external JS file, or you embed the JavaScript code right into your HTML page.
Both approaches will get the job done, but you should try to favor importing your code from an external file, as it's best to separate your HTML code from your JavaScript code for easy readability.
Oh! And don't forget, we're always offering SWEET stuff in the email signup form below. So if you haven't already, be sure to check out what freebies we're offering and sign up below.
The post EP33 – Importing JavaScript into HTML via Script tag appeared first on Coders Campus.