Explore this comprehensive tutorial on JavaScript, covering topics such as externalizing JavaScript, linking to the JavaScript file, and a step-by-step exercise on sharing JavaScript across multiple webpages.
This exercise is excerpted from Noble Desktop’s JavaScript for Front-End training materials and is compatible with JavaScript updates through 2023. To learn current skills in JavaScript with hands-on training, check out our Front-End Web Development Certificate, Full-Stack Web Development Certificate, and coding classes in-person and live online.
Topics covered in this JavaScript tutorial:
Externalizing JavaScript, Linking to the JavaScript file
Exercise Preview
Exercise Overview
In this exercise, we’ll learn how to externalize JavaScript so it can be shared across multiple webpages.
Getting Started
- 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 finished in the previous exercise.
- Switch back to your code editor.
- Open wall-clock.html from the Sharing-JavaScript folder.
-
Preview the page in a browser.
Notice this page looks the same and also has a color chooser, but it currently does not work because we have not added any JavaScript yet.
Because we’re already written the code to make a color chooser work, let’s reuse the code across both of these pages (imagining it could work across any number of product pages).
Externalizing JavaScript
If we copy and paste the JavaScript code into each page, we’ll end up with many copies, making updates difficult. Instead we’ll move the JavaScript code into a shared .js file, and link multiple HTML pages to it.
- 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!<script> let productPhoto = document.getElementById('product-photo');
Code Omitted To Save Space
} </script>
Cut the code.
Create a new file.
Paste the code into it.
Save the file as main.js into the js folder in the Sharing-JavaScript folder.
Linking to the JavaScript File
We need to link our HTML file to the external JavaScript file we just made.
- Switch back to chair.html.
-
Edit the tags as shown below (make sure to delete any extra lines and spaces between the opening and closing script tags):
<script src="js/main.js"></script> </body>
Save and preview chair.html in a browser.
Test out the color buttons to see they should still work.
- Switch back to your code editor.
- Switch to wall-clock.html.
-
At the bottom of the document, before the closing
</body>
tag add the following bold code:<script src="js/main.js"></script> </body>
Save the file.
Making the Code Work for Any Product
Our color chooser JS currently has the the chair product name hard-coded into the image path. To make this work with any product, we’re going to have to make a minor change.
-
Here in wall-clock.html, find the image with the id product-photo.
Notice it has a class of wall-clock. (In the other page, it has a class of chair.)
So the class is the product name, which we can grab to use in our JS so we’ll know which image to load.
- Switch to main.js.
-
In the changeColor() function, replace chair as follows:
productPhoto.src = 'img/' + productPhoto.className + '-' + this.id + '.jpg';
NOTE: Pay close attention to the single quotes and pluses.
- Save the file.
-
Preview wall-clock.html in a browser.
Hover the color buttons to see they now work!
Preview chair.html in a browser and see it still works too. Nice!