3

My script code is not being called when I load the nested HTML in my electron app and I cannot figure out why.

<div id="leftsidebar">

    </div>
    <div id="rightsidebar">

    </div>

    <div id="main" class="mainContent">
    </div>

    <script>

        const fs = require('fs');
        // Loads the html required for the side bar
        fs.readFile('leftSideBar.html', (err, data) => {
            document.getElementById('leftsidebar').innerHTML = data;
        });
        fs.readFile('rightSideBar.html', (err, data) => {
            document.getElementById('rightsidebar').innerHTML = data;
        });

        function loadMainWindowHTML(htmlString) {
            fs.readFile(htmlString, (err, data) => {
                document.getElementById('main').innerHTML = data;
            });
        }


    </script>

This is the main HTML code in my main window, and when I click on an item in my left side bar, it should execute loadMainWindowHTML() with the HTML file I want to open.


    <div id="myleftSidebar" class="leftSideBar">

        <a href="#" onclick="loadMainWindowHTML('searchUser.html')">Search User</a>

    </div>

The HTML file is called like this and loaded into the div with the id of main properly, however, the script in searchUser.html is never executed.

<head>

    <title>Document</title>
    <script>

        console.log("do we reach?");

    </script>
</head>

The log never appears anywhere. Any explanation or fix for this?

A.D
  • 157
  • 2
  • 13
  • 3
    Browsers ignore ` – Pointy Jun 03 '19 at 19:56
  • so in order to execute js, should i simply add a reference to a java script file in the head tags? – A.D Jun 03 '19 at 20:00
  • You could do that. What jQuery does (in some circumstances like this) is to explicitly strip out the inline ` – Pointy Jun 03 '19 at 20:03

1 Answers1

0

You can use any of the answers here, you cannot put script inside using innerHTML.my solution is to put the html and script in seperate files, and put the script in a new script element. Can scripts be inserted with innerHTML?

Sharvin K
  • 697
  • 3
  • 10