Code Functions in Codepen but Fails to Execute on My Personal Computer

Hey everyone, I recently posted a question here: Why My Javascript Slideshow Doesn't Function Properly

The codes work perfectly in StackOverflow and CodePen, but for some reason, they don't work on my computer. Can anyone assist me? What could be the reason behind this issue?

<html>
    <head>
        <link rel="stylesheet" href="path/to/font-awesome/css/font-awesome.min.css">
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">

        <meta name="viewport" content="width=device-width, initial-scale=1">
        
         // The rest of the HTML code can be retained as is
        
    </body>
</html>

Answer №1

After conducting some experiments, I finally cracked the code. It turns out that placing the javascript ABOVE the html caused querySelector to overlook your next/prev buttons, resulting in the variables holding them actually being set as null. By simply relocating the javascript to the bottom, this issue was resolved for me.

Alternatively, enclosing your javascript in window.onload = function() will also address the problem.

<html>
            <head>
                <link rel="stylesheet" href="path/to/font-awesome/css/font-awesome.min.css">
                <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
                <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
                <meta name="viewport" content="width=device-width, initial-scale=1">
                <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
                <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
                <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>

            <style>
                .nav{
                    width: 100%;
                    position: fixed;;
                    left: 0;
                    height:15%;
                    top:0px;
                    font-size:15px;
                    z-index:1000;
                }
                .a{
                    position: relative;
                    top:70%;
                    font-size: 20px;
                    text-decoration: none;  
                    font-family:'Lucida Sans', 'Lucida Sans Regular', 'Lucida Grande', 'Lucida Sans Unicode', Geneva, Verdana, sans-serif;
                    color:rgb(90, 76, 76);
                }
                ...
                // (Continues with styling rules)
                ... 
                           </script>
            </body>
        </html>

Answer №2

Ensure to declare script after the page has fully loaded

<html>
            <head>
                <link rel="stylesheet" href="path/to/font-awesome/css/font-awesome.min.css">
                <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
                <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
                <meta name="viewport" content="width=device-width, initial-scale=1">
                <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
                <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
                <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
            </head>
            <style>
                .nav{
                    width: 100%;
                    position: fixed;;
                    left: 0;
                    height:15%;
                    top:0px;
                    font-size:15px;
                    z-index:1000;
                }
                .a{
                    position: relative;
                    top:70%;
                    font-size: 20px;
                    text-decoration: none;  
                    font-family:'Lucida Sans', 'Lucida Sans Regular', 'Lucida Grande', 'Lucida Sans Unicode', Geneva, Verdana, sans-serif;
                    color:rgb(90, 76, 76);
                }
                ...
             </style>
            <body>
               
                <nav class="nav" id="navs">
                    <nav class="img1" id="imgg" ><span id="span">0 533 295 42 35</span><span id="span"><a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f2909e93909e93b2959f939b9edc919d9f">[email protected]</a></span></nav>
                    <div class="as">
                    <div class="sag" id="sag2">
                    <a class="a" id="abir">sad</a>
                    <a class="a" id="aiki">sad</a>
                </div>
                <div class="sol" id="sol">
                    <a class="a" id="auc">sad</a>
                    <a class="a" id="adort">sad</a>
                </div>
                </div>
                </nav>
                <div class="slider">
                    <div id="firstcontent"class="slide current">
                        <div class="content">
                            <h1>Slide One</h1>
                            <p>Slide One's paraghrap</p>
                        </div>
                    </div>
                   ...
             </script>
        </html>

Answer №3

It is highly recommended to place all Javascript code before the closing tag in your HTML document. This ensures that any dynamic Javascript code executed on page load will work correctly, as the Head section loads before the body. If Javascript code is placed just before the closing tag, any function calls made by the page during loading may result in errors.

All code is functioning properly.

https://codepen.io/r0binxp/pen/ExKPdmj

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

Sending a prop to a handler causes issues with navigation paths

I'm facing an issue with my handler and button component setup. Here's my handler: const addToCartHandler = (id) => { navigate(`/cart/${brand}/${id}?qty=${qty}`)}; And here's the button component using the handler: <Button onClick={a ...

Childnode value getting replaced in firebase

Every time I attempt to push values to the child node, they keep getting overridden. How can I solve this issue and successfully add a new value to the child node without being overwritten? ...

Tips on sending an Ajax POST request to execute a PHP file

//The code below is found in the inserirPF.js file function add(){ var method = 'AddPerson'; $.ajax({ url: "../class/dao/InserirPFDao.class.php", type: 'POST', data: {Method:method, NAME_:NAME, EMAIL_:EMAIL, PASS ...

Clicking a button in Angular JS to refresh a specific ID element

Just diving into AngularJS for my mobile web app development. I have different id divs for each view, as that's how AngularJS operates with one HTML page. My goal is to use specific javascript for each id page, as they need to retrieve JSON data objec ...

The hyperlink activation event is malfunctioning

There seems to be an issue with the "Goods" link not working after clicking on "Shops." <div class="i_find"> <div class="replaced_link">Goods</div> <div class="link_container"><a href="#" class="a_shops">Shops</a&g ...

Currently trapped within the confines of a Next.js 13 application directory, grappling with the implementation of a

I need to figure out how to export a variable from one component to layout.tsx in such a way that it is not exported as a function, which is currently causing the conditional check in the class name to always be true. Below is the code snippet: // File w ...

How to create a bottom border in several TD elements using a combination of CSS and jQuery

Check out my Website: Search Page I have written some CSS that functions well when searching by Name, but not by Specialty: .displayresult { display: block; } #fname, #lname, #splabel, #addlabel, #pnlabel { border-width: 4px; border-bottom-st ...

Troubleshooting the issue: Unable to shift a div to the left in a Rails app using jQuery and CSS when a mouseover event occurs

Hey there, I'm working on a div that contains a map. My goal is to have the width of the div change from 80% to 50% when a user hovers over it. This will create space on the right side for an image to appear. I've been looking up examples of jqu ...

Challenges with the final child element's CSS styling

Hey everyone, I've recently added the following CSS: #tab-navigation ul li:last-child { background-image: url(../images/tabs-end.gif); background-repeat: no-repeat; color: #fff; display: block; border: 1px solid red; backgrou ...

Setting array names in JSON using ReactJS can be achieved by simply assigning a variable to

In my constructor, I have initialized an empty array as shown below this.state = { formData: [], } When a user clicks a button, I am adding some data to this array using the following method addFormData() { const {formQuestion, formName} = th ...

Experiencing unexpected behavior with React Redux in combination with Next.js and NodeJS

I'm in the process of developing an application using Next.js along with redux by utilizing this particular example. Below is a snippet from my store.js: // REDUCERS const authReducer = (state = null, action) => { switch (action.type){ ...

Utilizing jQuery to Select Check Boxes Alongside Labels

In my jQuery code, I have a simple trigger set to run when a radio button is clicked in order to check certain checkboxes on the page. <script> jQuery('#op5').click(function () { $('input[type=checkbox]'). ...

Listening to a sequence of mp3 tracks consecutively

I'm currently working on a project that involves playing a series of mp3 files consecutively. However, I've encountered an issue with my code snippet below: Here's the code snippet: function playAudio(){ var au = document.getElementById( ...

Error: Unable to iterate through events using forEach method in Leaflet and VueJS due to TypeError

I am currently working on a Vue project and I want to incorporate Leaflet maps within my components. The map is displaying correctly, but I encounter an error when attempting to add a marker to the map. The specific error message that I receive is: Un ...

Which entity is responsible for supplying the next() function within Express middleware?

I'm trying to understand the workings of next() in Node.js and Express middleware. While there have been previous inquiries regarding middleware operations, such as this one, I am seeking a fresh perspective. The primary query troubling me is: who s ...

Leveraging node.js to power mobile applications along with an administration panel built using angular

Our current setup involves a mobile app that communicates with some APIs (no backend yet). I'm considering using node.js as a service for these APIs, even though I've never used it before being primarily a .net developer. For the administrative ...

Extract distinct values from a particular field and add them to a new array using an array of objects

I need a solution for the following issue. I attempted using underscore and groupBy, but it is producing an object of arrays with unique names inside an array. However, I am not satisfied with that result. Instead, I want something like the example below. ...

What is preventing me from updating the value of a variable in this manner?

Trying to understand the reason behind an issue with overwriting a value passed to an angularJS directive using an isolate scope (@). When attempting to change the value of vm.index with the following: vm.index = parseInt(vm.index, 10) It does not seem t ...

The onCall function in Firebase's realtime database does not provide a response data object when executing the remove() operation

When using onCall to perform a remove operation from a realtime database, I encountered an issue where the response only returned null instead of the expected data object. Strangely, I have used onCall for other operations like update and always received a ...

Learn how to efficiently import data into d3.js from several JavaScript arrays, and create a dynamically updating chart without the need to refresh the page

I currently have two arrays: one is a list of numbers called nums, and the other is a list of strings titled places. For example: nums=[1,2,3,4,5] places = ["house","gym", "work", "school", "park"] Both arrays are the same length. I am looking to crea ...