The JS content failed to load into the HTML page

I need help creating a workout tracker using two JS files. The main.js file is responsible for loading content from the second file (workoutTracker.js) into the index.html. However, I'm facing an issue where the content is not being displayed on the page. Any suggestions or guidance would be greatly appreciated. Feel free to ask for more clarification if needed.

HTML:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Fitness Tracker</title>
    <link rel="stylesheet" href="style.css" />
    <script href="main.js" type="module"></script>
    <script href="workoutTracker.js" type="module"></script>
  </head>
  <body>
    <div id="app">
      <!-- Content goes here -->
    </div>
  </body>
</html>

JS (main.js):

import workoutTracker from "./workoutTracker.js";

const app = document.getElementById("app");

new workoutTracker(app);

JS (workoutTracker.js):

export default class workoutTracker {
  constructor(root) {
    this.root = root;
    this.root.insertAdjacentHTML("afterbegin", workoutTracker.html());
    this.loadEntries();
    this.updateView();
  }
  static html() {
    return `
    <table class="tracker">
      <thead>
        <tr>
          <th>Date</th>
          <th>Workout</th>
          <th>Duration</th>
          <th></th>
        </tr>
      </thead>
      <tbody class="tracker__entries">
      </tbody>
      <tbody>
        <tr class="tracker__row tracker__row--add">
          <td colspan="4">
            <button class="tracker__add">Add Entry &plus;</button>
          </td>
        </tr>
      </tbody>
    </table>
    `;
  }
}

Answer №1

The reason is that the script tag includes src="" instead of href="". You need to replace href with src.

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

Answer №2

Switch out "Script href" for "Script src". Additionally, ensure that these script elements are placed above the closing "body" tag (similar to the example below).

<script src="main.js" type="module"></script>
<script src="workoutTracker.js" type="module"></script>
</body>

Answer №3

To optimize the loading of modules in your script, consider including the main script with a defer attribute. This attribute instructs the browser to defer the execution of the script until after all HTML has finished parsing. Alternatively, you can place the script at the bottom before the closing body tag to ensure it only loads once all the HTML is parsed (in this case, the defer attribute is not necessary).

     <script  src="./main.js" type="module"></script>
    <!DOCTYPE html>
    <html lang="en">
        <head>
            <meta charset="UTF-8" />
            <meta http-equiv="X-UA-Compatible" content="IE=edge" />
            <meta name="viewport" content="width=device-width, initial-scale=1.0" />
            <title>Fitness Tracker</title>
            <link rel="stylesheet" href="style.css" />
            <!-- Remember to use defer attribute or place the script at the end of the body for optimal performance -->
            <script  src="./main.js" type="module"></script> 
        </head>
        <body>
            <div id="app"></div>
        </body>
    </html>

Ensure that you are correctly loading the workoutTracker.js module as needed based on the provided snippet.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Customize your JQuery/HTML5 Slider to accept user input

Currently, I am developing a time tracking mobile application in MVC4, which includes the use of a slider input type. <input type="range" name="time_verdi" id="slider" value="7.5" min="0.0" max="24" step="0.5" data-highlight="true"/> When using thi ...

I'm looking to customize the text displayed on the screen from my JSON file. Is there a way to hide this text?

I am currently working with a JSON data file that contains values and displays them on the screen alongside a CSS property width. However, I do not want the output to be the actual numerical data; instead, I aim to connect the JSON file so that the value s ...

Tips for preventing the collapse of a table row using a button?

I have implemented a Bootstrap collapse feature on my table row. Currently, the collapse can be triggered by clicking anywhere on the row, which is working as intended. However, I would like to make one exception - if the user clicks on the desktop icon i ...

Removing an element from an array in a Laravel database using AngularJS

I am currently working on a project where I need to delete an item from my Laravel database, which is a simple Todo-List application. To achieve this, I have implemented a TodosController in my main.js file using AngularJS and created a Rest API to connect ...

View complex response objects in Postman as easily digestible tables

I am interested in displaying the data provided below as a table using Postman Tests. The table should have columns for product, price, and quantity, with Items listed in rows. It's important to note that there may be multiple shippingGroups within th ...

Efficiently handling multiple form submissions using a single jQuery Ajax request

I'm working on a page that has 3-4 forms within divs, and I want to submit them all with just one script. Additionally, I want to refresh the content of the specific div where the form is located. However, I'm unsure of how to specify the current ...

Mastering Cookies in Javascript

I have been exploring the world of cookies in Javascript and decided to create an experimental log-in page. The page is fully functional, but I am interested in displaying the user's username and password using cookies. Is this possible with Javascrip ...

After stopping the interval with clearInterval(), you can then use the res.send method

I need to continuously log the current date and time to the server console then stop logging after a specified time, returning the final date and time to the user. How do I properly utilize ClearInterval() in this scenario? const express = require(" ...

Exploring etoro data through python web scraping

Attempting to automate the process of logging into my etoro account and extracting data from my portfolio using Selenium. Currently working on Google Colab, here is what I have so far: from selenium import webdriver options = webdriver.ChromeOptions() opt ...

Chrome debugging tool does not refresh page source

This issue has been lingering for quite some time and despite similar questions, I have not come across a satisfactory solution. The problem lies in the fact that the SOURCE used to step through the code does not refresh with every page load. Disabling the ...

Ways to navigate to a new page using jQuery Mobile

As a newcomer to jquery mobile, I am still learning how ajax handles page transitions, and I suspect the issue may be related to that. In my jqm / phonegap app, I have set up multiple pages. The homepage checks if the user is logged in by retrieving membe ...

Is it possible to implement a goto-like functionality in Node.js or how can one define and invoke a function inside an asynchronous function?

Utilizing puppeteer, I have set up an automated test to fill out a form and verify captcha. In the event that the captcha is incorrect, the web page refreshes with a new image. However, this requires me to reprocess the new image in order to reach the func ...

When attempting to execute a function when a hidden div is not displayed, JQuery encounters an

I'm currently attempting to modify the text color of the h2 title with the id ticketSearchTitle when the ticketSearch div is not visible. However, the code I have been using to achieve this seems to cause issues (such as the absence of a pointer icon ...

Initiating an AJAX call with custom headers

According to David Flanagan's book "JavaScript: The Definitive Guide, 5th Edition", it is recommended to send request headers before making an AJAX request. Is it necessary to do this for compatibility across different browsers? request.setRequestHe ...

Input field that adjusts its width based on screen size, accompanied by two buttons displayed side

I need a solution to display a text input and two buttons side by side. The buttons should take up only the space they require, while the text input should expand to fill the remaining space. It's important to have small margins between these elements ...

I have developed a custom jQuery carousel that includes functionality to start and stop the carousel based on specific conditions

I have set up a jQuery carousel that moves to the left when a checkbox is checked, but I need it to stop moving when the checkbox is unchecked. Can someone help me achieve this? $("#checkBox").click(function(){ if($(this).prop("checked") == true){ ...

Transferring information from the router to the server file using Node.js and Express

I am in the process of creating a web application using node.js, and I need to transfer data from my router file, which handles form processing, to my server file responsible for emitting events to other connected users. router.js module.exports=function ...

What is the best way to modify a newly added element using jQuery?

When a user clicks a button on my screen, a new template is loaded dynamically using jQuery's .load() method. This transition adds new HTML to the page that was not present when the script initially loaded: (function($) { $('.go').on("c ...

Automatically reset the form after submission in CakePHP 1.3

I've encountered an issue with my report form that is linked multiple times on a page to a jQuery dialog box. To prevent cross-posting, I added a random string to the submission process. After successfully submitting the form on a single page access, ...

Changing the input value in Nuxt / Vue 2 after it has been overridden

I'm working on a feature where the user's typed keys are converted to the last single upper-cased character (for example, 'a' becomes 'A', 'abc' becomes 'C'). Here is my current code snippet: <template& ...