-1

I'm new to Javascript and I'm trying to create a simple ElectronJS app. However, I can't get external scripts to work. It should be added that both files are in the same folder (as are all of the application files). What am I missing here?

This works:

main.html

<head>
    <script>
    function color_red() {
        document.getElementById("collectBtn").style.color = "red";
    }
    </script>
</head>
<body>
    <button id="collectBtn" onclick="color_red()">Color me!</button>
</body>

This does not:

connect.js

function color_red() {
    document.getElementById("collectBtn").style.color = "red";
}

main.html

<head>
    <script src="connect.js"></script>
</head>
<body>
    <button id="collectBtn" onclick="color_red()">Color me!</button>
</body>
Daniel Slätt
  • 751
  • 2
  • 15
  • 28
  • 1
    Are both the files under same folder? Check in your sources tab to see whether the linked JS resource has been loaded or not. – Rajaprabhu Aravindasamy Dec 19 '18 at 11:43
  • Well, I work in Sublime text and it is able to detect the function in the linked file when looking inside the .html. Not sure if that was what you were referring to? – Daniel Slätt Dec 19 '18 at 11:50
  • 1
    Look in the console and see if the script has been loaded, I'm assuming not (which will show as a 404 error, and a red cross - at least in Chrome). Then check to see what location it was trying to fetch the script from, and see if that corresponds to where you're expecting it to be. – Robin Zigmond Dec 19 '18 at 11:54
  • 1
    if you close the `` it'll work – pergy Dec 19 '18 at 12:08
  • Sorry, a typo on my part - the script has been closed in my original file. – Daniel Slätt Dec 19 '18 at 13:26
  • I'm running the application on the cmd of Windows, I can't see any error messages in the cmd when running it. – Daniel Slätt Dec 19 '18 at 13:27
  • `I'm running the application on the cmd of Windows` - from this it sounds like you've got some server-side code that you're running locally, in which case we really need details of your whole setup to know exactly what's going on. (You still haven't responded to my earlier comment regarding if there were any 404s in the browser console, and what URL it was trying to get the script from.) – Robin Zigmond Dec 19 '18 at 14:01

1 Answers1

2

As @pergy says as the <script> tag can have content in it should be closed in order to work.

<script src="connect.js"></script>

However if you are just starting with Electron I recommend you to take a look at this answer to help you understand how to integrate better function calls in your application:

Button click event binding in Electron js

Pelayo Méndez
  • 389
  • 4
  • 10