You link JavaScript to HTML in three ways: a <script> tag in your HTML file pointing to an external .js file, a <script> tag with code written directly inside it, or an event attribute on an HTML element

The most common approach is to create a separate JavaScript file and link it from your HTML using a <script> tag. This keeps your code organized and lets you reuse the same JavaScript file across multiple HTML pages. You place the <script> tag near the bottom of your HTML file, just before the closing </body> tag, and point it to your .js file using the src attribute.

If you only need a small amount of JavaScript, you can write it directly inside a <script> tag in your HTML file instead of creating a separate file. For very specific interactions, you can also put JavaScript directly into HTML element attributes like onclick or onmouseover, though this approach becomes messy as your code grows.

Key Takeaways

  • Link an external JavaScript file by placing <script src="filename.js"></script> near the bottom of your HTML file, just before </body>.
  • Write JavaScript directly in your HTML file by putting code between <script> and </script> tags, useful for small amounts of code.
  • Placing the <script> tag at the bottom of your HTML file ensures the page loads before JavaScript runs, preventing errors.
  • External JavaScript files are the best choice for larger projects because they keep your code organized and let you use the same file on multiple pages.

Linking an External JavaScript File

Create a new file with a .js extension — for example, script.js — and save it in the same folder as your HTML file or in a subfolder. Write your JavaScript code in this file. Then, in your HTML file, add a <script> tag with a src attribute that points to your JavaScript file.

Here is what it looks like in your HTML:

<!DOCTYPE html> <html> <head>   <title>My Page</title> </head> <body>   <h1>Hello World</h1>   <script src="script.js"></script> </body> </html>

The src attribute tells the browser where to find your JavaScript file. If your JavaScript file is in a subfolder called js, write src="js/script.js" instead. The browser reads your HTML from top to bottom, so placing the <script> tag at the bottom ensures all your HTML elements load before the JavaScript runs.

Writing JavaScript Directly in Your HTML File

For small scripts, you can write JavaScript directly inside your HTML file using opening and closing <script> tags. Place these tags in the <head> section or at the bottom of the <body> section, depending on when you need the code to run.

Here is an example:

<!DOCTYPE html> <html> <head>   <title>My Page</title> </head> <body>   <h1>Hello World</h1>   <script>     console.log("This runs when the page loads");   </script> </body> </html>

This method works well for testing or for code you only use once. As your project grows, keeping JavaScript in separate files becomes easier to manage.

Using Event Attributes to Run JavaScript

You can trigger JavaScript by adding attributes directly to HTML elements. The most common is onclick, which runs JavaScript when someone clicks an element. Other event attributes include onmouseover (when the mouse enters an element), onmouseout (when the mouse leaves), and onload (when the page finishes loading).

Here is an example with a button:

<button onclick="alert('Button clicked!')">Click Me</button>

For anything more than a single line of code, this approach becomes hard to read and maintain. It is better to write a function in a separate JavaScript file and call that function from the event attribute instead.

Where to Place Your Script Tag

Placing your <script> tag at the bottom of your HTML file, just before the closing </body> tag, is the standard practice. This ensures that all your HTML elements load first, so your JavaScript can find and interact with them without errors.

If you place the <script> tag in the <head> section, the browser will try to run your JavaScript before the HTML elements below it have loaded. This can cause errors if your JavaScript tries to interact with elements that do not exist yet. You can work around this by using the defer attribute on your <script> tag, which tells the browser to wait until the page loads before running the script.

Here is an example with the defer attribute:

<head>   <script src="script.js" defer></script> </head>

Organizing Multiple JavaScript Files

As your project grows, you may need multiple JavaScript files. You can link as many as you want by adding multiple <script> tags. The browser runs them in the order they appear in your HTML file, so if one file depends on another, list them in the correct order.

A common structure looks like this:

<script src="js/helpers.js"></script> <script src="js/main.js"></script> <script src="js/animations.js"></script>

If main.js uses functions from helpers.js, make sure helpers.js loads first. Create a folder called js in your project and keep all your JavaScript files there to stay organized.

Checking That Your JavaScript Is Connected

Open your HTML file in a web browser and right-click anywhere on the page. Select "Inspect" or "Inspect Element" to open the browser's developer tools. Click the "Console" tab to see if any errors appear. If your JavaScript is connected correctly and has no errors, the console will be empty or show only messages your code intentionally prints.

If you see an error like "Cannot find element" or "Function is not defined," your JavaScript may be running before your HTML elements load, or your file path may be wrong. Double-check that your src attribute points to the correct file location and that your <script> tag is at the bottom of your HTML file.

Frequently Asked Questions

Can I put the script tag in the head section instead of at the bottom?

Yes, but it can cause problems. If your JavaScript tries to interact with HTML elements that have not loaded yet, you will get errors. Use the defer attribute on your <script> tag if you put it in the head — this tells the browser to wait until the page loads before running the script.

What is the difference between src and inline script tags?

A <script src="file.js"></script> tag loads an external file, while <script>code here</script> runs code written directly in your HTML. External files are better for larger projects because you can reuse them across multiple pages and keep your code organized.

Do I need to close the script tag?

Yes, always use a closing </script> tag, even when using the src attribute. Write <script src="file.js"></script>, not <script src="file.js" />.

What if my JavaScript file is in a different folder?

Use a relative path in the src attribute. If your JavaScript file is in a subfolder called js, write src="js/script.js". If it is in a parent folder, write src="../script.js". The two dots mean "go up one folder level."

Can I link JavaScript from a website instead of a local file?

Yes. You can link to JavaScript libraries hosted online by using their full URL in the src attribute, like src="https://example.com/library.js". This is common for popular libraries, but your page will only work if you have an internet connection.