Issues with JavaScript scripts functionality on my live server

Hey everyone, I'm new to this community and having some trouble with my index.html file. Everything works fine except for one script that only runs when I open the file directly with live server in VSCode. Can someone help me out? The script causing issues is located at the end of the second code block.

<!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>HTML Exercises</title>
    <style>
        #conteudo {
            padding: 30px 0px;
            font-size: 20px;
        }
    </style>
</head>
<body>
    <header>
        <h1>HTML Exercices</h1>
    </header>

    <nav>
        <a href="exercises/option1.html"> 01 - Most Used Tags</a> 
        <a href="exercises/option2.html"> 02 - Mouse</a> 
        <a href="exercises/option3.html"> 03 - Other tags</a> 
        <a href="exercises/lists.html"> 04 - Lists</a> 
        <a href="exercises/nestedList.html"> 05 - Nested Lists</a> 
        <a href="exercises/links.html"> 06 - LINKS</a>


    </nav>

    <section section id = 'conteudo'></section>
    <footer> <br> Modern Web Course</footer>

    <script>
        document.querySelectorAll('a').forEach(link => {
            const conteudo = document.getElementById('conteudo')
            
            link.onclick = function(e){
                e.preventDefault()
            fetch(link.href)
                .then(resp => resp.text())
                .then(html => conteudo.innerHTML = html)
        }})
    </script>
</body>
</html>
<h1>Project</h1>
<ul class = 'tree'>
    <li>
        <span wm-folder> backend</span>
        <ul>
            <li>
                <span wm-folder>app</span>
                <ul>
                    <li>product.js</li>
                    <li>user.js</li>
                    <li>sale.js</li>
                </ul>
                <li>
                    <span wm-folder>config</span>
                    <ul>
                        <li>db.js</li>
                        <li>routes.js</li>
                        <li>server.js</li>
                    </ul>
                </li>
            </li>
        </ul>
    </li>
    <li>
        <span wm-folder>frontend</span>
        <ul>
            <li>
                <span wm-folder>publics</span>
                <ul>
                    <li>rapp.css</li>
                    <li>mouse.css</li>
                    <li>mole.css</li>

                </ul>
            </li>
        </ul>
    </li>
</ul>

<link rel="stylesheet" href="http://files.cod3r.com.br/web-course/tree.css">

<script>
    document.querySelectorAll('[wm-folder]').forEach(folder => {
        folder.onclick = function (e){
            console.log('test')
            const ul = folder.nextElementSibling
            const display = ul.style.display
            ul.style.display = display == 'none' ? 'block' : 'none'
        }
    }) 
</script>

I've tried clearing cache on Opera GX browser and disabling adblock, but nothing seems to fix the issue.

Answer №1

When using the innerHTML to inject script elements inside another element, they will not be executed. An alternative could be to utilize an <iframe> to display the content instead.

<style>
#content {
padding: 30px 0px;
font-size: 20px;
width: 100%;
border: none;
}
</style>
<!-- ... -->
<iframe id='content'></iframe>
<!-- ... -->
<script>
const content = document.getElementById('content');
document.querySelectorAll('a').forEach(link => {
link.addEventListener('click', e => {
e.preventDefault();
content.src = link.href;
});
});
</script>

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

Tooltips remain inactive on the first plot until there is a change in the plot, triggering their functionality

I've encountered a strange issue with tooltips using d3-tip in a scatter plot. Initially, the tooltips are not functioning at all, and even the mouseover events are not triggering as expected. The scatter plot consists of three sets of data on each a ...

Navigating a table list in React using arrow keys without inadvertently scrolling the scrollbar

Currently, I've created a table component that contains a list of items. I've implemented hotkeys using the react-hotkeys package to allow users to navigate through the list using the 'arrow up' and 'arrow down' keys. The issu ...

Can someone provide guidance on utilizing parentheses in HTML code?

I am facing an issue in my Angular project while using the WordPress API. The problem lies in the API address structure which includes "" like this: <img src="{{importantvideo.better_featured_image.media_details.sizes.["covernews-med ...

Exploring the functionalities of JavaScript methods and nested components within Vue.js

Learning Vue.js has been an interesting experience for me. However, I am facing challenges with methods and child elements in the library. It seems like there is something simple that I am overlooking. In my current project, I have list items rendered on ...

triggering a function from a child component in React

I am facing a challenge in calling a function from the parent component that is stored in a child component. I understand how to do it from child to parent using props, but unsure about how to achieve it from parent to child. In the example below, you can ...

Getting data from components in React: A step-by-step guide

As a newcomer to Reactjs, I find myself working on an existing project where I am attempting to retrieve product reviews using components. However, my console currently displays 0 reviews. How can I resolve this issue? The file in question is "pages/[slug ...

Guide on setting up a MEAN stack application to run on port 8080

I am brand new to the mean stack development environment. I'm attempting to configure my root domain name to display the app directory once I enter the command grunt, but the only way it currently works is at website.com:8080/!#/. How can I get it to ...

Dynamic cell editing feature in PrimeNG table

Looking to implement the PrimeNG Table. https://i.stack.imgur.com/bQycr.png Check out the live demo on StackBlitz. The table has three editable columns. The "Property Name" column always displays a text box in edit mode, while the "Property Value Type" ...

What could be causing my image not to show up on ReactJS?

I'm new to ReactJS and I am trying to display a simple image on my practice web app, but it's not showing up. I thought my code was correct, but apparently not. Below is the content of my index.html file: <!DOCTYPE html> <html> & ...

Make the div in jQuery clickable to redirect to the child URL

Could someone shed some light on why this code isn't functioning as expected? I'm fairly new to javascript and jQuery. Below is the snippet in question: Javascript / jQuery: <script type="text/javascript"> $('.clickk').cl ...

Failed to instantiate npm module when installing external AngularJS library

While working on my AngularJS project, I decided to integrate an external library, such as chart.js. I began by trying to add this new package to my node_modules with the following command: npm install chart.js --save However, during the download process, ...

Transforming dynamic tables into JSON format

Whenever a user adds a new row to the dynamic table to input customer information, I require the data to be submitted in JSON format upon clicking the submit button. HTML <table class="table table-bordered table-hover" id="driver"> ...

Which option is more effective: using an id or using !important?

While some say to avoid using the !important rule and others argue that since ids are unique, there is no need to target like #main > #child and you can simply use #child. In my particular case: #main > div{ color: red; } #child{ color: blue;/ ...

Display a q-table inside a nested row q-table component

In order to display lists within items in another list, such as a list of chapters with their titles, I am trying to achieve this using q-tables instead of q-list. Although it is usually straightforward with q-list, I am facing some challenges while worki ...

Error encountered in Node/Express application: EJS partials used with Angular, causing Uncaught ReferenceError: angular is not defined

I seem to be missing something important here as I attempt to incorporate ejs partials into a single-page Angular app. Every time I try, I encounter an Uncaught ReferenceError: angular is not defined in my partial. It seems like using ejs partials instead ...

Getting callback data from a function in the AWS SDK without using asynchronous methods

I have a code snippet that fetches data from AWS using a function: main.js const { GetInstancesByName } = require("./functions"); var operationmode = "getinstances"; if (operationmode == "getinstances") { let getresult = ...

Having trouble with getting the footer to display correctly on mobile when dealing with a scrollable section within another scrollable section in React and CSS

Looking for some assistance with a coding issue I've encountered. I have created a section on my website where I map components, which includes a vertical scroll. Outside of this section, there are other components such as a map, navbar, footer, etc. ...

The content spills out of the container

I'm currently working with a div that displays scrolling text when it overflows. I am wondering if there is a way to hide the scroll bars until the text actually overflows? Additionally, is there a method to make the overflow occur only vertically and ...

Issues arise with selecting text and highlighting it in JavaScript functions

I am currently working on a fun little web application that allows users to select text and highlight it in any color they choose. Here is the progress I have made so far: function replaceSelectedText() { var sel, range; var replacementText, spanT ...

Errors in the console appear when jQuery loads HTML containing images

Whenever I attempt to load a simple piece of HTML (though some images are currently inaccessible), jQuery seems to be attempting to load them, resulting in errors in the console. Here is an example of the code: var html = $('<div>' + &apo ...