Sharing JavaScript Across Pages

Free JavaScript & jQuery Tutorial

Explore the process of externalizing JavaScript and linking HTML files to it, enabling shared use of a single JavaScript file across multiple pages, through this detailed JavaScript & jQuery tutorial.

This exercise is excerpted from Noble Desktop’s past JavaScript training materials. Noble Desktop now teaches JavaScript and the MERN Stack in our Full Stack Development Certificate. To learn current skills in web development, check out our coding bootcamps in NYC and live online.

Note: These materials are provided to give prospective students a sense of how we structure our class exercises and supplementary materials. During the course, you will get access to the accompanying class files, live instructor demonstrations, and hands-on instruction.

Topics covered in this JavaScript & jQuery tutorial:

Externalizing JavaScript, Linking to the JavaScript file

Full-Stack Web Development Certificate: Live & Hands-on, In NYC or Online, 0% Financing, 1-on-1 Mentoring, Free Retake, Job Prep. Named a Top Bootcamp by Forbes, Fortune, & Time Out. Noble Desktop. Learn More.

Exercise Preview

externalizing js preview

Exercise Overview

In this exercise, we’ll learn how to externalize JavaScript so it can be shared between pages.

Getting Started

  1. Open your code editor if it isn’t already open.
  2. Close any files you may have open.
  3. For this exercise we’ll be working with the Sharing-JavaScript folder located in Desktop > Class Files > yourname-JavaScript jQuery Class. You may want to open that folder in your code editor if it allows you to (like Visual Studio Code does).
  4. Open index.html from the Sharing-JavaScript folder.
  5. Preview the page in a browser. Notice on the right of this page, that there is the accordion you coded in the previous exercise. Test it out to see that it’s working.
  6. Switch back to your code editor.
  7. Open photo-gallery.html from the Sharing-JavaScript folder.
  8. Preview the page in a browser. Notice this page also has the accordion on the right, but it’s not working because it doesn’t have any JavaScript.

Externalizing JavaScript

The photo gallery needs the same JavaScript code we currently have on the homepage (index.html). If we copy and paste the JavaScript code we’ll end up with two copies, making later updates harder. As a better alternative, we’ll move the JavaScript code into a shared JavaScript (.js) file, and link both HTML pages to it.

  1. Switch back to your code editor.
  2. Switch back to index.html.
  3. Find the script tag around line 86.
  4. Select all the code between the opening and closing script tags. Don’t select the <script> and </script> tags!

    <script>
       document.getElementById('comingUpContent').style.display = 'none';
    

    Code Omitted To Save Space

    
       function showEventsWeek() {
    

    Code Omitted To Save Space

       }
       function showComingUp() {
    

    Code Omitted To Save Space

       }
       function showPastEvents() {
    

    Code Omitted To Save Space

       }
       function hidePanels() {
    

    Code Omitted To Save Space

       }
    </script>
    
  5. Cut the code.

  6. Create a new file.

  7. Paste the code into it.

  8. 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.

  1. Switch back to index.html.
  2. Scroll to the bottom and find the empty script tags around line 86.
  3. 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>
    
  4. Save and preview the file. Test out the accordion to see it should still be working.
  5. Switch back to your code editor.
  6. Switch to photo-gallery.html.
  7. At the bottom of the document, before the closing </body> tag add the following bold code:

    </div>
    <script src="js/main.js"></script> 
    </body>
    
  8. Save and preview the file in a browser. Test out the accordion to see it is now working on this page as well. Sweet.

    NOTE: If you want to refer to our final code example, go to Desktop > Class Files > yourname-JavaScript jQuery Class > Done-Files > Sharing-JavaScript.

External JavaScript

It’s a best practice to externalize JavaScript and you should always do so when you’re ready to go live. For the sake of simplicity, however, we’ll continue to write JavaScript inside our HTML documents for the next few exercises. Later in the course we’ll work with external JavaScript.

How to Learn Coding

Master coding with hands-on training. Learning how to code in JavaScript, Python, and other popular languages can pave the way to a job in tech, such as web development, data science & analytics, or software engineering.

Yelp Facebook LinkedIn YouTube Twitter Instagram