Topics Covered in This JavaScript Tutorial:
Externalizing JavaScript, Linking to the JavaScript File
Exercise Preview

This tutorial demonstrates transforming embedded JavaScript into reusable external files, using a product color chooser that works across multiple webpage types.
Exercise Overview
In this exercise, you'll master a fundamental JavaScript best practice: externalizing your code into reusable modules. By moving JavaScript from inline script tags into external files, you'll create maintainable, scalable solutions that can power multiple webpages efficiently. This approach is essential for modern web development, where code reusability and clean architecture separate professional applications from amateur implementations.
Getting Started
Let's begin by setting up your development environment and examining the existing code structure.
- For this exercise we'll be working with the Sharing-JavaScript folder located in Desktop > Class Files > JavaScript Class. Open that folder in your code editor if it allows you to (like Visual Studio Code does).
- In your code editor, open chair.html from the Sharing-JavaScript folder.
- Preview chair.html in a web browser.
This is the product color chooser you completed in the previous exercise—a fully functional interactive component that demonstrates JavaScript's power in creating dynamic user experiences.
- Switch back to your code editor.
- Open wall-clock.html from the Sharing-JavaScript folder.
Preview the page in a browser.
Notice this page has identical styling and structure, complete with a color chooser interface, but the interactive functionality is missing—the buttons don't respond because no JavaScript has been implemented yet.
Rather than duplicating our existing code (a violation of the DRY principle—Don't Repeat Yourself), we'll demonstrate how to architect reusable JavaScript that can power unlimited product pages while maintaining a single source of truth.
Project Setup Requirements
Locate Project Files
Open the Sharing-JavaScript folder from Desktop > Class Files > JavaScript Class in your code editor
Examine Existing Pages
Review chair.html with working color chooser and wall-clock.html without JavaScript functionality
Identify Code Duplication
Recognize the need to share JavaScript functionality between multiple product pages
Externalizing JavaScript
Code duplication is the enemy of maintainable software. When JavaScript lives directly in HTML files, every update requires hunting down and modifying multiple locations—a recipe for bugs and inconsistencies. By externalizing our JavaScript into dedicated .js files, we create a centralized codebase that's easier to debug, update, and scale. This practice is standard across professional development teams and essential for any serious web application.
- Switch back to your code editor.
- Switch back to chair.html.
- Find the script tag at the bottom.
Select all the code between the opening and closing script tags. Do not select the
<script>and</script>tags themselves!<script> let productPhoto = document.getElementById('product-photo');Code Omitted To Save Space
} </script>Cut the code (Ctrl+X or Cmd+X).
Create a new file in your editor.
Paste the JavaScript code into this new file.
Save the file as main.js into the js folder within the Sharing-JavaScript directory.
You've now created your first external JavaScript module—a standalone file that can be linked to any HTML page requiring this functionality.
External JavaScript Files vs Inline Code
When extracting JavaScript code, copy only the content between script tags, not the opening and closing script tags themselves.
Linking to the JavaScript File
With our JavaScript externalized, we need to establish the connection between our HTML documents and the new .js file. This linking process tells the browser where to find and execute our code.
- Switch back to chair.html.
Modify the script tags as shown below, ensuring you remove any content between the opening and closing tags:
<script src="js/main.js"></script> </body>Save and preview chair.html in a browser.
Test the color selection buttons—they should function exactly as before. This confirms that external JavaScript files execute with the same capabilities as inline code, but with significantly better organization.
- Switch back to your code editor.
- Switch to wall-clock.html.
At the bottom of the document, immediately before the closing
</body>tag, add the following code:<script src="js/main.js"></script> </body>Save the file.
Script Tag Implementation Methods
| Feature | Inline JavaScript | External JavaScript |
|---|---|---|
| Code Location | Inside HTML file | Separate .js file |
| HTML Syntax | <script>code here</script> | <script src="path/file.js"></script> |
| Reusability | Single page only | Multiple pages |
| Maintenance | Update each page | Update one file |
Making the Code Work for Any Product
Currently, our color chooser contains hardcoded references to the "chair" product, limiting its reusability. Professional JavaScript applications use dynamic approaches that adapt to different contexts automatically. We'll refactor our code to detect the product type programmatically, creating truly flexible functionality.
In wall-clock.html, locate the image element with id product-photo.
Observe that it includes a class attribute set to wall-clock, while the corresponding element in chair.html uses chair. This class name serves as our product identifier—a semantic approach that makes our HTML self-documenting and our JavaScript context-aware.
- Switch to main.js in your editor.
Within the changeColor() function, replace the hardcoded "chair" reference with dynamic class detection:
productPhoto.src = 'img/' + productPhoto.className + '-' + this.id + '.jpg';NOTE: Pay careful attention to the single quotes and plus operators—JavaScript string concatenation requires precise syntax.
- Save the file.
Preview wall-clock.html in a browser.
Hover over the color buttons—they now respond dynamically, loading the appropriate wall-clock images based on your selections.
Test chair.html in your browser to verify it continues working flawlessly.
Congratulations! You've successfully created a scalable, reusable JavaScript component that exemplifies modern development practices. This pattern—externalizing code, using semantic HTML attributes for context, and writing dynamic rather than hardcoded solutions—forms the foundation of professional web applications that can grow and adapt over time.
productPhoto.src = 'img/ ' + productPhoto.className + ' -' + this.id + '.jpg';Code Flexibility Implementation
Use element properties like className to make code adaptable
Verify that dynamic code works for chair.html and wall-clock.html
Ensure all product pages use the same class naming convention
Check that dynamic paths correctly reference image files