What is causing the javascript code in my html to not function properly?

I have been testing my site in vscode, but the section where the javascript should be displayed appears blank when running it. I even tried using a console.log message for testing purposes, but that is also not showing up.

This is where I am working on adding a progress bar:

    <p class="infoBox">
            <h3>Skills</h3> 
            <h4>Computer Languages</h4>
            Java  <div id="container"></div></br>
            Python</br> 

This is the javascript code I am using:

<script type="text/javascript" src="/require.js">
    console.log("Hello World!"); // Testing js functionality - not appearing in chrome's console

    var ProgressBar = require('progressbar.js');

   var bar = new ProgressBar.Line(container, {
        strokeWidth: 4,
        easing: 'easeInOut',
        duration: 1400,
        color: '#FFEA82',
        trailColor: '#eee',
        trailWidth: 1,
        svgStyle: {width: '100%', height: '100%'}
      });

      bar.animate(1.0);
</script>

Below is the CSS being used:

#container {
  margin: 20px;
  width: 400px;
  height: 8px;
}

Here is what displays when I run the site.

Answer №1

It seems like there was a mix-up in your code. When you use

<script src=""></script>
, you are essentially telling the browser to import a JavaScript library (such as require.js). Anything between the opening and closing <script> tags will be ignored.

If you want to actually execute JavaScript code, you have two options:

  1. Option 1: Inline Javascript example:

<script src="require.js"></script>
<script>
  console.log("test")
</script>

  1. Option 2: Create your own .js file and reference it in your HTML:

<script src="require.js"></script>
<script src="your_own_js_file.js"></script>

Answer №2

When the src attribute is present within a tag, it can prevent the contents from being read properly. To fix this issue, just delete the src attribute and everything should function correctly.

Answer №3

The variable container in your source appears to be undefined, which is why you are attempting to set the progress bar to an empty container. To fix this issue, either assign the value '#container' to your variable or replace new ProgressBar.Line(container, with

new ProgressBar.Line("#container",

Additionally, make sure to separate the HTML and JavaScript portions of your code like so:

<script src="/require.js"/>
<script>
  Your script here
</script>

Answer №4

To ensure that the progress bar renders correctly using the div element's ID, you should load the JavaScript code after the window has finished loading.

index.js

window.onload = function onLoad() {
  var bar = new ProgressBar.Line(container, {
    strokeWidth: 4,
    easing: 'easeInOut',
    duration: 1400,
    color: '#FFEA82',
    trailColor: '#eee',
    trailWidth: 1,
    svgStyle: { width: '100%', height: '100%' },
  });

  bar.animate(1.0);
};
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <title>ProgressBar.js - Minimal Example</title>
    <meta name="viewport" content="width=device-width, initial-scale=1" />

    <style>
      #container {
        margin: 20px;
        width: 400px;
        height: 8px;
      }
    </style>
  </head>
  <body>
    <p class="infoBox"></p>
    <h3>Skills</h3>
    <h4>Computer Languages</h4>
    <p>Java</p>
    <div id="container"></div>
    <p>Python</p>
    <script src="https://cdn.rawgit.com/kimmobrunfeldt/progressbar.js/0.5.6/dist/progressbar.js"></script>
    <script src="index.js"></script>
  </body>
</html>

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

Modify the content of a table cell once the hyperlink in a different cell on the corresponding row is selected

I currently have a table that displays items pulled from a database. Each item has a checkbox to mark it as "Valid", which successfully updates the status in the database. However, I am looking for a way to dynamically update the text on the page after the ...

The issue with jQuery's .after() method is that it is inserting content as a string instead

Having trouble inserting a button element after another element selected using jQuery. Here's how I have my element selected: let btn = $("a[translate='server.ipmi.kvm.LAUNCH']")[0]; When I try to insert the button after it, I'm using ...

Can Vue2-Google-Maps dynamically load API keys using props?

Is there a way to access the component props before it gets rendered? I want to dynamically load the Google Maps API based on a passed prop value. import * as VueGoogleMaps from 'vue2-google-maps'; import GmapCluster from 'vue2-google-maps/ ...

Adding a jPlayer on the fly

I've been working on a code snippet to dynamically add jPlayers through a function. Here is the code I have so far: function audio_player(audio, title, type) { var id = $('.audio').length; $('#audio').append('<di ...

Grabbing <object> HTML using jQuery

One example on my webpage is the presence of the following <object>: <object id="obj1" data="URL"></object> Could you please suggest a method to access this html object using jQuery? ...

Utilizing the push method to add a JavaScript object to an array may not be effective in specific scenarios

When I tried using users.push within the 'db.each' function in the code below, it didn't work. However, moving the 'users.push' outside of it seemed to do the trick. Is there a way to successfully push the new objects from db.each ...

Extract keys from the string

Is there a better way to extract keys from a string? const {Builder, By, Key, until} = require('selenium-webdriver'); ... const obj = {val: 'Key.SPACE'} if(obj.val.startsWith('Key.'))obj.val = eval(obj.val); (...).sendKeys(obj ...

React Material-UI TextField with Throttling

After exploring React Material UI, I am interested in implementing a TextField component that offers both debouncing and controlled functionality. When we refer to "controlled," we mean that the value persistence is managed externally, with the component a ...

Angular 6: Issue with displaying data on the user interface

Hello! I am attempting to fetch and display a single data entry by ID from an API. Here is the current setup: API GET Method: app.get('/movies/:id', (req, res) => { const id = req.params.id; request('https://api.themoviedb.org/ ...

Javascript - WebSocket client encountering issues with onmessage event not triggering upon receiving a large response from the server

My task involves creating a .NET WebSocket server using TcpClient and NetworkStream to communicate with a JavaScript client. The client initiates the connection and requests specific actions from the server, which then generates responses. Previously, thes ...

Choosing a Date using a DateTimePicker

I recently implemented a directive in my HTML for selecting dates, and I'm struggling to add time slots to it. Here is the code snippet: JSFIDDLE angular .module('App',['ui.date']) .directive('customDatepicker', ...

Angular encounters an issue where it is unable to access a property within a directive

Currently, I am utilizing the angular-media-player module from https://github.com/mrgamer/angular-media-player. Here is how my HTML code looks: <audio media-player="audioControl" data-playlist="list" a="{{audioControl.ended}}"> I am able to access ...

Ember.js alternative for Angular's filter for searching through ng-models

Looking for an easy way to implement a search filter similar to Angular? <input type="text" ng-model="resultFilter" placeholder="Search"> <ul> <li ng-repeat="result in results | filter:resultFilter">{{result.name}}</li> </u ...

Display the keyboard on IOS when an input field is selected

I'm facing an issue that seems to have no solution. When using input.focus() on IOS, the keyboard doesn't appear searchMobileToggle.addEventListener('click', function() { setTimeout(function(){ searchField.focus(); ...

View saved local storage information

I have an inquiry regarding saving data using localStorage for a shopping cart feature. I am able to list the data on one page through console.log but am facing difficulties in calling the data on another page. Can anyone assist with this? The Product page ...

The CSS code ".btn btn-primary * { visibility: hidden; } is failing to work as intended

Trying to hide specific buttons for print using CSS, but it doesn't seem to be working. Here is the code snippet: .btn btn-primary * { visibility: hidden; } <div id="control" style="display: none"> <button class="bt ...

"Troubleshooting the failure of the alert function to work properly when loading content

I am working on a webpage named index.html <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Co ...

Is it possible to verify if an event occurred within a specific hour, regardless of the date using Moment.js?

I am in the process of monitoring the usage patterns of a device and displaying users their usage trends by the hour. Essentially, I need to analyze times documents to determine if the device was active between specific hour intervals, such as 6pm to 7pm, ...

What is the best way to remove text messages from a channel that only allows images?

I have developed a Discord bot named YES with a specific text channel for images only. My goal is to program the bot to automatically delete any text messages in this channel and respond with "You cannot send text messages." However, I also want to create ...

The functionality of two-way binding in a distinct controller is not functioning properly when integrated with angular-wizard

I've been working hard to integrate this amazing wizard controller into my project: However, I've hit a roadblock with two-way binding not functioning as expected outside of the <section> attribute: http://plnkr.co/edit/N2lFrBRmRqPkHhUBfn ...