Node.js causing issues with executing jQuery front-end code properly

I created an automatic image scroller div consisting of a ul with small images. The aim is to have a basic scroller that starts running once the page loads. It functions perfectly when I test the code (HTML, CSS, and a JS file with jQuery) locally in a browser, but fails to execute correctly when the application runs on a node server.

Attempts to troubleshoot include ensuring that jQuery is properly included and wrapping the main code that controls the scroller in a $(window).on("load") function. Many similar posts I've researched suggest this might be the issue. Additionally, I verified that the server is loading the external file, confirmed by console logs showing up without any errors. Checking the chrome console reveals the required images and external script are being loaded onto the page. Further investigation by logging values inside the external function indicates that the resources are accessible, so perhaps it's a synchronization issue with jQuery + node? Any assistance would be greatly appreciated.

The jQuery

$(window).on("load", function () {
   $(function () {
       var scroller = $('#scroller div.innerScrollArea');
       var scrollerContent = scroller.children('ul');
       scrollerContent.children().clone().appendTo(scrollerContent);
       var curX = 0;
       scrollerContent.children().each(function () {
           var $this = $(this);
           $this.css('left', curX);
           curX += $this.outerWidth(true);
       });
       var fullW = curX / 2;
       var viewportW = scroller.width();

       var controller = { curSpeed: 0, fullSpeed: 1 };
       var $controller = $(controller);
       var tweenToNewSpeed = function (newSpeed, duration) {
           if (duration === undefined) {
               duration = 600;
               $controller.stop().animate({ curSpeed: newSpeed }, duration);
           }

       };
       var doScroll = function () {
           var curX = scroller.scrollLeft();
           var newX = curX + controller.curSpeed;
           if (newX > fullW * 2 - viewportW)
               newX -= fullW;
           scroller.scrollLeft(newX);

       };
       setInterval(doScroll, 20);
       tweenToNewSpeed(controller.fullSpeed);
       console.log("foo");
       console.log(scrollerContent);

   });
});

Server File

var express = require("express");
var db = require("./models/company.js");

var app = express();
var PORT = process.env.PORT || 8080;

app.use(express.urlencoded({ extended: false }));
app.use(express.json());
app.use(express.static( __dirname + "/public"));

require("./routes/htmlRoutes.js")(app);
require("./routes/apiRoutes.js")(app);

db.sequelize.sync({ force: false }).then(function() {
  app.listen(PORT, function () {
    console.log("App listening on PORT: " + PORT);
  });
})
module.exports = app;

Related CSS

#scroller {
    position: relative;
}
#scroller .innerScrollArea {
    overflow: hidden;
    position: absolute;
    left: 0;
    right: 0;
    top: 0;
    bottom: 0;
}
#scroller ul {
    padding: 0;
    margin: 0;
    position: relative;
}
#scroller li {
    padding: 0;
    margin: 0;
    list-style-type: none;
    position: absolute;
    margin-left: 1%;
    margin-right: 1%;
}

relevant code from index.html

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
        integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
    <link rel="stylesheet" href="assets/css/index.css">


    <title>RepoMan</title>
</head>
...
    <!--AD Space-->
    <div id="scroller" style="width: 1110px; height: 75px; margin: 0 auto;">
        <div class="innerScrollArea">
            <ul>
                <!-- Define photos here -->
                <li><img class="ad-img" src="assets/images/sidebar/sm-allied.gif" /></li>
                <li><img class="ad-img" src="assets/images/sidebar/sm-baja.gif" /></li>
                <li><img class="ad-img" src="assets/images/sidebar/sm-bigtime.png" /></li>
                <li><img class="ad-img" src="assets/images/sidebar/sm-det.png" /></li>
                <li><img class="ad-img" src="assets/images/sidebar/sm-dyn.gif" /></li>
                <li><img class="ad-img" src="assets/images/sidebar/sm-ken.png" /></li>
                <li><img class="ad-img" src="assets/images/sidebar/sm-locating.png" /></li>
                <li><img class="ad-img" src="assets/images/sidebar/sm-minuteman.png" /></li>
                <li><img class="ad-img" src="assets/images/sidebar/sm-nams.jpeg" /></li>
                <li><img class="ad-img" src="assets/images/sidebar/sm-rec.png" /></li>
                <li><img class="ad-img" src="assets/images/sidebar/sm-recmng.gif" /></li>
                <li><img class="ad-img" src="assets/images/sidebar/sm-repfl.png" /></li>
                <li><img class="ad-img" src="assets/images/sidebar/sm-rsig.gif" /></li>
            </ul>
        </div>
    </div>
...


    <script src="https://code.jquery.com/jquery-3.4.1.js"
        integrity="sha256-WpOohJOqMqqyKL9FccASB9O0KwACQJpFTUBLTYOVvVU=" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"
        integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1"
        crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"
        integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM"
        crossorigin="anonymous"></script>
    <script src="assets/js/index.js"></script>



</body>

</html>

package.json

{
  "name": "UW2",
  "version": "1.0.0",
  "description": "",
  "main": "index.html",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node server.js",
    "dev": "nodemon server.js"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "dotenv": "^8.2.0",
    "express": "^4.17.1",
    "jquery": "^3.4.1",
    "mysql2": "^1.7.0",
    "nodemon": "^1.19.4",
    "sequelize": "^5.21.1"
  }
}

Answer №1

Remember to add the line

<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
within your index.html file.

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

Restricting choices once user selects an option (HTML)

Currently, I am faced with a dilemma regarding two sets of options for an HTML form. One set consists of various units (such as cm, in, ft.), while the other set includes different locations. The selection of a unit would restrict certain location option ...

Stopping the Vimeo player at the end of every Cycle slide

I am currently working on a project where I need to pause Vimeo players in each Cycle slide. I have successfully achieved this by declaring them individually in my script here: $(function() { var iframe1 = $('#player1')[0]; var iframe2 = ...

Utilize AJAX to update the database through a bootstrap modal interface

My current project involves creating a webpage to display database records with edit buttons that trigger bootstrap modals for user input and status changes. The goal is to use AJAX to update the database with any modifications made. However, I'm enco ...

Storing an array from a different schema or object in MongoDB using the _id field, while incorporating Joi

Currently, I am working on developing a backend service using node.js for a potential portfolio. This service is meant to manage projects posted on the platform. I intend to design the application with a separate model to store categories, and have my proj ...

Grab the webpage's URL by collecting the link from the src attribute of the script tag using XSLT

Can XSLT be used to extract a URL link specified within the src attribute of a script tag in an HTML file? Here is an example of the HTML file: <HTML> <BODY> <SCRIPT language="javascript" src="http://myspace.com" type="text/javascript"> ...

Tips for executing a loop while waiting for a jQuery ajax request to complete

I currently have this code setup: for (var i = 0; i < $total_files; i++) { $.ajax({ type: 'POST', url: 'uploading.php', context: $(this), dataType: 'json', cache: false, contentType: false, pr ...

Transmitting an array of data from React to Express/Node in order to conduct a

Struggling to transmit an array of ID numbers from React state, through Node/Express, and ultimately into MongoDB. The primary obstacle lies in figuring out how to send the array from React to the server. Once it's in a usable array format on the ser ...

Is it possible to display a div when hovering over it and have it remain visible until

How can I make the div ".socialIconsShow" fade in when hovering over ".socialIcons", and then fade out when hovering over ".socialIconsShow"? Is there a way to keep the icons visible even after leaving ".socialIcons"? <div class="socialNetworks"> ...

Step-by-step guide to setting up Angular 2 with fullpage.js scrolloverflow

I am currently working on a project using Angular 2 that incorporates the fullpage.js port from https://github.com/meiblorn/ngx-fullpage. I am facing an issue with implementing scrolloverflow on a specific section and could use some guidance. I have alread ...

Verification symbols in Unicode

I'm on the hunt for a universal checkmark symbol that is compatible across various operating systems and web browsers. I've tested out the following two options: Version 1: ✓ Version 2: ✔ (source) However, these symbols seem to be working ...

Is it possible to run a React, Vite, Node.js, and Express app all at once

I am currently learning React and JavaScript, but I am facing challenges when it comes to connecting my frontend and backend. I have been attempting to run both the front and back end simultaneously in order to send requests to the backend and display fetc ...

issue arising where the table's border is not displaying

I'm confused about why the border of the first tbody tr's td is not visible. https://i.stack.imgur.com/Fwlv7.png I can't see any reason why the border is not showing up. Here's what I've figured out: If the natural height of th ...

What is the reason behind the effectiveness of NPM's policy regarding duplicate dependencies?

When utilizing NPM to manage a package that relies on both foo and bar, which in turn depend on corelib, often corelib will be installed twice by default (once for foo and once for bar), potentially resulting in different versions of corelib being used. C ...

What is the best way to choose a date and time in a custom format within my React application?

I'm currently developing the front-end of my application using React. I am looking for a way to capture the date and time in the specific format shown in this image. Any suggestions or solutions would be greatly appreciated! ...

Error: Unable to retrieve the specified ID

One unique aspect of my backbonejs app is the model structure: var Store = Backbone.Model.extend({ urlRoot: "/stores/" + this.id }); This is complemented by a router setup like so: var StoreRouter = Backbone.Router.extend({ routes: { 's ...

What is the best way to pass the text of a selected option in a dropdown menu to a function when a button is clicked in AngularJS?

Alright, so I've got this HTML page with a select element. The select has three options defined in the $scope below: <div id="mainID" ng-controller="theController"> <form name="assetTypeForm" class="form-horizontal" role="form" novalidat ...

how to use ajax to retrieve a value returned by a php function

I have two different files, one named index.php and the other called get_content.php. Strangely, I am unable to display anything on the get_content.php file. I am now left pondering where the issue might be - in index.php or get_content.php? To view the F ...

Generate CSV file reports quickly and seamlessly without causing lag on a node.js server

Currently, I have a Node.js express API running on an Azure App Service that retrieves data from an SQL server database. The new requirement involves creating customisable CSV reports on demand. The challenge lies in the potential size of these CSV files, ...

Create a rectangular container with text using CSS

Looking to achieve a square box design using CSS with specific dimensions? Want to insert text inside the square and ensure it is horizontally centered within the square? Check out this code snippet: #myid { width: 100px; height: 100px; ...

The stick division shows some bouncy behavior when seen on a mobile device

When viewing on mobile, the #main-categories div seems to be jumping instead of smoothly sticking to the top. What could be causing this behavior? Here is the code snippet I have: var s = $("#main-categories"); var pos = s.position(); $(window).scroll(f ...