Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to include javascript in HTML

Understanding the Basics of JavaScript and HTML

JavaScript is a scripting language used to enhance and manipulate web pages. This language introduces interactivity to otherwise static HTML web pages. In this post, we'll be looking at how to include JavaScript in HTML files.

Let's picture JavaScript as a wizard who can change the elements on a webpage according to your wishes. HTML, on the other hand, is the architecture or structure of a house. Without JavaScript, HTML is just a static structure. So, let's invite this wizard into our HTML house!

Including JavaScript Directly in HTML

The simplest way to include JavaScript in an HTML file is by using the <script> tag. This tag can be placed anywhere within the <head> or <body> tags. Here's an example:

<script>
alert("Hello, World!");
</script>

This code displays a pop-up alert box with the message "Hello, World!" when the HTML file is loaded in a browser. The <script> tag encapsulates the JavaScript code, much like a container holding the magic spells of our JavaScript wizard.

However, this method can become cumbersome for larger scripts or when the same script is used across multiple HTML files.

Linking to External JavaScript Files

To keep your HTML and JavaScript clean and organized, it's often better to write your JavaScript in a separate file and then reference that file in your HTML. This is similar to hiring multiple wizards (JavaScript files) to work on different parts of your house (HTML file).

Here's how you do it:

  1. Create a separate file with a .js extension. Let's call it main.js.
  2. Write your JavaScript code in this file. For example:
alert("Hello, World!");
  1. In your HTML file, use the <script src=""> tag to link to your .js file. For example:
<script src="main.js"></script>

The src attribute specifies the path to the JavaScript file.

Positioning of Script Tags

The position of your script tags can affect how your webpage loads and behaves. If you place your script tags in the <head> section, your scripts will load before your HTML. This might cause issues if your script relies on HTML elements which haven't loaded yet.

On the other hand, placing your script tags at the end of your <body> tag ensures that your HTML content loads first. This is generally recommended especially when your script interacts with HTML elements.

Understanding Asynchronous and Defer Attributes

The async and defer attributes are used to control how JavaScript is executed. These attributes are like telling your wizard when and how to cast their spells.

  • async: This attribute downloads the file during HTML parsing and will pause the HTML parser to execute it when it has finished downloading.
<script async src="main.js"></script>
  • defer: This attribute downloads the file during HTML parsing and will only execute it after the parser has completed.
<script defer src="main.js"></script>

Both async and defer are useful when your scripts are large and could potentially block the rendering of your web page.

Conclusion

Incorporating JavaScript into your HTML files is like inviting a wizard into your house to make it come alive. Whether you choose to include JavaScript directly in your HTML file or link to external JavaScript files, understanding how and where to place your script tags and how to use the async and defer attributes can greatly improve the performance and behavior of your web pages.

Remember, practice makes perfect. The more you work with JavaScript and HTML, the more comfortable you'll become. So, go ahead and practice implementing JavaScript in different ways. Happy coding!