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

Position a div in the center of the page horizontally

Can someone help me with aligning the #main div in the center of the page? I have this code snippet: <div id="main" style=" margin:0 auto; "> <div style="float: left"> <a style="display: block" href="#"><img src="test.gif" ...

What is the process for adjusting the timeout in a jasmine-node asynchronous test?

Is there a way to successfully pass this test without using runs or waitsFor blocks? it("should not modify timeout settings", function(done) { request("http://localhost:3000/hello", function(error, response, body){ expect(body).toEqual(" ...

Why does my element appear to be a different color than expected?

I've developed a sleek wind map (check out for reference). Using a weighted interpolation every 10ms, I generate uVector and vVector data to calculate wind speed. Based on this speed, each point is assigned a specific color as outlined below. if (we ...

The TextField component in MUIv5 is showing some frustrating white space in the corners

I've integrated the MaterialUI_v5 library and included a TextField component within a Paper component. The background of the Paper component has been customized to appear in a shade of green. For the Textfield component, I have applied a styling tha ...

``Only Firefox supports jQuery animations, all other browsers fail to render them properly

This particular issue is a bit complex to articulate, so I'll begin by providing the code and then proceed to elaborate on the problem as best as I can. I'm assuming that you've already compared the results in Firefox and other browsers. He ...

What is the reason behind Firefox's disregard of CSS font-size for code tags within pre tags?

There seems to be a discrepancy in how different browsers on Mac OS X 10.11.4 handle font-size. Is this an issue with Firefox, a CSS bug, or am I missing something about CSS? Here's a JSFiddle where you can see the details. In a section like this: &l ...

Having trouble with importing SendInBlue into my NodeJS application?

Currently in the process of updating my Node app to utilize ES6 import modules over requiring files. Encountering difficulties while trying to integrate this change with SendInBlue for email functionality, resulting in the following error message: TypeEr ...

What is the best way to pass a module from the controller to a Jade/Pug template and then use it as an argument in an event handler within the template?

I passed the module 'naija' to a template from the controller in this way: res.render('testing', {title:"filter setting page", nigeria:naija }); The func ...

Having trouble processing the Firebase snapshot with Node.js

I have a question regarding a snapshot; ref.orderByChild("index").equalTo(currentIndex).once("value", function(snapshot) {}) After printing the snapshot with ; console.log(snapshot.val()); This is the output that gets printed; {'-LBHEpgffPTQnxWIT ...

Adjust the position of the element by lifting it slightly

I am looking to adjust the positioning of the number "01 / 04" to match the layout shown in this image: Here is what I have accomplished so far: This is how the code structure looks like in vue js at the moment: <img id="theZoomImage" sizes="100vw" : ...

Multer-SFTP did not report any errors and the file failed to upload

I'm encountering an issue with this storage setup. Despite configuring everything correctly and being able to connect to my SFTP server using the same credentials through WinSCP or Putty, when I try to upload a file through this library, there are no ...

Stacking pictures with CSS and absolute placement

I have designed a webpage where I need to layer three images in a specific order to create a background for content placement without any movement during scrolling. Fixed positioning is not suitable for this purpose. Below is the sequence in which the imag ...

Node.js cluster dedicated to handling a particular task within an Express application

Exploring ways to incorporate a Node.js cluster into my Express app for a specific function. Starting from a standard Express app generated with the express app generator. The initial task of my app involves scraping an eCommerce website to gather catego ...

Extract the element when the mouse is clicked

I'm currently working on developing a Chrome extension with a specific goal in mind: My aim is to capture the username when a user Ctrl-clicks on a username while browsing Reddit, and then transfer that username from the content script page to the ba ...

Displaying and concealing a div based on the scroll position

I have implemented a script that hides a div and then shows it when I scroll past a certain point on the page. It is working correctly, but once I scroll back up to the top, the div remains visible. Can someone suggest a modification to the code that will ...

Row in Internet Explorer 7

I have a function that reveals hidden rows in a table, which works perfectly in all tested browsers except for IE7. The function is written using Prototype.js. function showInactives(){ var row_list = $$('tr.inactive'); var ck =$('inactive_ ...

Achievement with ajax: If the status code is 200, execute one function; otherwise, execute a

I can't figure out why this isn't working... I'm using $.ajax to run file.php and pass a POST value from an input field. Even though the file.php is functioning properly (it successfully adds a user to my newsletter), my ajax function seems ...

Changes in menu layout in response to window resizing

The menu needs to be centered on the website and adjust to the browser window resizing. Currently, it's positioned in the center and the animation is working fine. However, when I attempt to make the menu responsive so that it stays centered when resi ...

Instructions on using $.post in jquery to sude uploaded file to a php file for processing

Is there a way to upload photos asynchronously using $.post without relying on .serializeArray()? I want to send a form, containing an upload file, to a PHP processing file. Any suggestions? HTML: <form action="upload1.php" method="post" id="usrform" ...

Understanding image sizes for uploads on Tumblr can be a bit confusing, especially when comparing pages to posts. Learn how to implement lazyloading for post

I'm currently working on a highly customized Tumblr account that features a mix of pages and posts. I am looking to access content and assets, particularly images, from these pages/posts for use in other parts of the site. When I upload an image to a ...