Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to include external JavaScript in html

Getting Started: HTML and JavaScript

At the core of any website or web application, you'll find HTML (HyperText Markup Language) and JavaScript working together. HTML is the skeleton of a webpage, providing the structure, while JavaScript is the muscle, allowing for interactivity and dynamic content.

In this article, we will dive deeper into how you can include external JavaScript files into your HTML document. Think of it as attaching a robotic arm to our skeleton, giving it the ability to interact with the world.

What is an External JavaScript File?

In the realm of JavaScript, there are three ways to include it in your HTML file: inline, internal, and external. Inline and internal JavaScript are written directly within the HTML file. However, an external JavaScript file is a separate file that is linked to the HTML file.

Imagine you have a toolbox (your JavaScript file) that you want to use in different houses (your HTML pages). Instead of carrying the toolbox into each house and leaving it there, an external file allows you to leave the toolbox in your car (external file) and bring in only the tools you need for each house. This makes your HTML file cleaner, easier to read, and more efficient.

Now, let's see how you can link this toolbox to your HTML file.

Including External JavaScript in HTML

To include an external JavaScript file in your HTML document, you will need to use the <script> tag. This tag is the bridge that connects your HTML file to the JavaScript file. Here's an example:

<!DOCTYPE html>
<html>
<head>
  <title>My First Web Page</title>
  <script src="myscript.js"></script>
</head>
<body>
  <h1>Welcome to my Web Page</h1>
</body>
</html>

In the code above, the <script> tag has an attribute called src (or source), which is set to "myscript.js". This is the path to the JavaScript file. If the JavaScript file is in the same directory as the HTML file, you can just write the file name. If it's in a different directory, you would need to include the path to the file, such as "./js/myscript.js".

Take note that the <script> tag is placed in the <head> section of the HTML document. This means that the JavaScript file will be loaded before the body of the HTML document. It's like telling your webpage to first pick up the toolbox from the car before starting any work.

When Should You Load Your JavaScript?

However, loading JavaScript in the <head> can sometimes lead to issues. For example, if your JavaScript code tries to manipulate or change an element in the body of your HTML document, it won't work because the body hasn't loaded yet.

This is like trying to fix a sink before you've entered the house. The solution is to place your <script> tag just before the closing </body> tag. Here's how to do it:

<!DOCTYPE html>
<html>
<head>
  <title>My First Web Page</title>
</head>
<body>
  <h1>Welcome to my Web Page</h1>
  <script src="myscript.js"></script>
</body>
</html>

Now, your JavaScript code will only run after the entire HTML document has loaded.

Optimizing JavaScript Loading

There is another attribute that you can add to the <script> tag to optimize the loading of your JavaScript file: async. This attribute allows the browser to continue parsing the HTML document while the JavaScript file is being downloaded. This is like having a friend bring the toolbox while you start inspecting the house. Here's how to use it:

<!DOCTYPE html>
<html>
<head>
  <title>My First Web Page</title>
</head>
<body>
  <h1>Welcome to my Web Page</h1>
  <script async src="myscript.js"></script>
</body>
</html>

However, using async can lead to issues if your JavaScript file depends on another JavaScript file. In this case, you can use the defer attribute, which tells the browser to defer the execution of the JavaScript file until after the HTML document has been fully parsed.

In Conclusion

Including external JavaScript files in your HTML document is like hiring a team of robotic arms for your skeleton. It gives your webpage the ability to interact, to respond, and to evolve, all the while keeping your toolbox tidy and your workflow efficient. As you continue your journey in programming, remember that every language, every tool, and every script you learn is yet another powerful arm you're adding to your team. Keep building, keep experimenting, and never stop learning. After all, the world of programming is only as limited as your imagination.