Javascript and React - Scripts

JavaScript Essentials: Connecting HTML with Dynamic Functionality

Learning the fundamentals of connecting JavaScript with HTML opens up a world of possibilities for creating interactive and dynamic web pages. In this guide, we’ll explore how to integrate JavaScript with HTML, from basic setup to creating interactive elements that respond to user input.

Setting Up JavaScript in Your HTML

The foundation of connecting JavaScript with HTML starts with properly linking your script file to your HTML document. This connection is established using a simple but crucial tag:

<script type="text/javascript" src="script.js"></script>

Script tag placement

Your script tag must be placed inside of <body> tag, preferably at the end, right before closing </body> tag.

<!DOCTYPE html>
<html>
<head>
    <title>My Page</title>
</head>
<body>
    <h1>Welcome</h1>
    <div id="area">Content here</div>
    
    <!-- Script goes here, at the END of body -->
    <script type="text/javascript" src="script.js"></script>
</body>
</html>

Why NOT on the <head> tag?

When you place the script in the <head>, the browser executes it before the HTML body elements are created. This causes problems:

  1. Elements don’t exist yet: If your JavaScript tries to access elements like document.getElementById('area'), it will fail because the <div id="area"> hasn’t been created yet!
  2. Variables become inaccessible: The timing of script execution affects variable scope and console access.
  3. Page loads slower: The browser stops rendering HTML to execute JavaScript first, making your page appear slower.

The solution: Place the script at the end of <body> so all HTML elements are already loaded when JavaScript runs.

Important: Your JavaScript file must be in the same directory as your HTML file. If it’s located elsewhere, you’ll need to specify the complete file path in the src attribute.

Understanding variables and the console

Once your JavaScript file is connected, you can start creating variables and testing them through the browser’s console. To verify that your script is working correctly, create a simple variable:

var name = "Hermione";

Then open your browser’s developer console and call this variable to confirm everything is functioning properly!

Writing comments in JavaScript

Documentation is key to maintainable code. JavaScript offers two ways to write comments:

  • Single-line comments: Use // for brief notes
  • Multi-line comments: Use /* your comment here */ for longer explanations

Key JavaScript objects

Two fundamental objects you’ll work with constantly:

  • document: Represents your HTML page
  • window: Represents the entire browser window

Dynamic content with document.write()

The document.write() method allows you to add content directly to your webpage. You can insert plain text, HTML elements, or even images:

// Adding text with HTML tags
document.write("<h1>Welcome to My Site</h1>");

// Adding an image
document.write("<img src='http://kidcoders.io/wp-content/uploads/2025/10/HTML-JS.jpg'/>");

This method can be executed directly in the browser console for quick testing.

Creating Interactive Buttons

Buttons bring interactivity to your website. Here are two powerful examples:

Redirect Button

Create a button that navigates to another website. On the index.html file:

<button onclick="window.location.href='https://music.youtube.com';">Visit YouTube Music</button>

Refresh Button

Create a button that reloads the current page:

<button onclick="window.location.href=window.location.href;">Reload Page</button>

Modifying HTML Content Dynamically

The <div> element serves as a container for organizing content. When combined with JavaScript, it becomes a powerful tool for dynamic updates.

Creating a Dynamic Welcome Area

Start with a div that has a unique ID:

<div id="area">
    Welcome...
</div>

Then create a button that modifies this content:

<button onclick="document.getElementById('area').innerHTML = prompt('What is your name?');">Enter</button>

Pro tip: To add to the existing content instead of replacing it, concatenate strings:

document.getElementById('area').innerHTML = 'Welcome ' + prompt('What is your name?');

Self-Modifying Buttons

Create buttons that can change their own text:

<button onclick="this.innerHTML = prompt('What should the button say?');">What's my name?</button>

The keyword this refers to the button element itself, allowing it to modify its own properties.

Putting It All Together

These fundamental concepts form the building blocks of interactive web development. By combining HTML structure with JavaScript functionality, you can create engaging user experiences that respond to input, display dynamic content, and navigate between pages.

The key to mastering these concepts is practice. Start with simple examples, experiment in your browser’s console, and gradually build more complex interactions. Remember: every dynamic website you’ve ever used relies on these same fundamental principles.

See you on the next one! 🙂