≡ Menu

Importing JavaScript into HTML via Script tag

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.

How to Link HTML and JavaScript Together

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:

  1. Importing an external JavaScript file into your HTML page
  2. Embedding JavaScript code directly onto your HTML page

Now that you know there are two ways to link your JavaScript to your HTML, let’s take a look at each approach.

Importing JavaScript into an HTML Page

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:

<!DOCTYPE HTML>
<html>
  <head>
    <title>A Simple HTML Page</title>
  </head>
  <body>
    <h1>My HTML Page</h2>
  </body>
</html>

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://howtoprogramwithjava.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 <script> tag with a src property that points to the file itself.

Here’s what that script tag would look like:

<script type="text/javascript" src="https://htpwj-zone1-rrzzqutxbqkmxn.netdna-ssl.com/wp-content/uploads/2015/07/Debugging_Example.js"></script>

SUPER IMPORTANT NOTE: When using the script tag, it is mandatory that you use the closing </script> tag just as it appears. Don’t try to use the “short-cut” approach like this <script type="text/javascript" />. This will cause issues and will likely end in your script NOT being imported at all.

Alright, having said that, let’s put all of this code together and import this JavaScript file into our simple HTML page!

It’s usually recommended that you put your script tags into the head area of your page like so:

<!DOCTYPE HTML>
<html>
  <head>
    <title>A Simple HTML Page</title>
    <script type="text/javascript" src="https://htpwj-zone1-rrzzqutxbqkmxn.netdna-ssl.com/wp-content/uploads/2015/07/Debugging_Example.js"></script>
  </head>
  <body>
    <h1>My HTML Page</h2>
  </body>
</html>

And voila! You’ve now successfully imported the “Debugging_Example.js” file into your simple HTML page.

Pretty simple to understand I hope.

Embedding JavaScript Code in Your HTML Page

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 </script> 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:

<!DOCTYPE HTML>
<html>
  <head>
    <title>A Simple HTML Page</title>
    <script type="text/javascript" >
      console.log("The words you're seeing here are coming from a JavaScript file that I've loaded");
      console.log("You'll be able to debug this JavaScript file in your browser");
      console.log("For the sake of seeing debugging in action, let's use a FOR loop to count to 10");
      
      for (var i=0; i<10; i++)
      {
        console.log(i);
      }
      console.log("And now we're done counting, hope you enjoyed it!");
    </script>
  </head>
  <body>
    <h1>My HTML Page</h2>
  </body>
</html>

Boom! You've just learned how to directly embed JavaScript code into your HTML pages.

When to Import and When to Embed

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 Summary

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.

{ 1 comment… add one }

  • , Canadá, Irlanda, Inglaterra, Francia, Alemania…

    Tampoco podemos dejar a un lado su programa de Trimestre Académico en Australia y e Irlanda, campamentos de verano urbanos, campamento de verano internacional, campamento políglota,
    campamento intensivo de inglés, Este curso de Tenis esta diseñado para todo tipo
    de niveles, desde principiante hasta avanzado.

Leave a Comment