JavaScript functions stored in electron

I recently completed a fresh electron application.

The application is made up of the following files:

index.html
main.js
renderer.js

Inside the index.html file, I included a button with an onclick event:

<button onclick="myfunction()">Call Function</button>

The function associated with the button is a simple alert for testing purposes:

myfunction() {
    alert('I am the js function');
}

However, I encountered an issue where I added the function code in both the renderer.js file and at the end of main.js, yet when I click the button, the alert does not appear.

Is there a way for me to ensure that js functions are called correctly from the index.html file?

Answer №1

Consider organizing the myFunction in a standalone javascript file. This file can be named "functions.js". To incorporate this function into the HTML file (index.html), you can insert the Javascript code by utilizing the script tag in the subsequent manner -

<html>
  <body> 
  ...
  </body>
  <script type="text/javascript" src="./functions.js"></script>
</html>

Next, it is important to assign an onclick listener to the button produced in the HTML code.

In order to achieve this, include the following script in functions.js

document.getElementById('btn').addEventListener("click", function(){
    myFunction();
});

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

Using CKEditor in an AngularJS web application: Tips and tricks

I'm having trouble integrating the ckeditor into an HTML page that's built with angularjs. Despite trying out numerous examples, such as the ng-ckeditor and ckeditor directives, I haven't found a solution that works for me. What I need is ...

Step-by-step guide on duplicating a div a set number of times

I am looking to replicate the entire div multiple times (e.g., on an A4 paper, top left corner, top right corner, bottom left corner, and bottom right corner). <script language="javascript" type='text/javascript'> function updateD ...

React input field keeps losing focus during re-render operations

My current project involves using React to create an input text that displays a value from an in-memory data store and updates the store when the input value changes, triggering a re-render. However, I am facing an issue where the input text loses focus du ...

Creating CSS for a specific class on a single webpage can be achieved by targeting that class

Here is my custom style: <style> ul { padding:0 0 0 0; margin:0 0 0 0; } ul li { list-style:none; margin-bottom:25px; } ul li img { cursor: poin ...

Creating intricate JavaScript objects for JSON API integration can be accomplished by following these steps:

Here is a sample JSON structure used for querying an API: "order_items": [ { "menu_item_id": "VD1PIEBIIG", "menu_item_name": "Create Your Own", "modifiers": [ { "modifier_id ...

Verify whether a certain key exists within the gun.js file

Suppose I need to verify whether a specific entry exists in the database before adding it. I attempted the following: gun.get('demograph').once((data, key) => { console.log("realtime updates 1:", data); }); However, I only receive ...

Exploring the depths of web automation using Python and Selenium, harnessing the power of a while

Currently, I am navigating through various pages on iens website using Selenium and Python. My goal is to click on the "Volgende" button (which means "next" in Dutch) continuously until there are no more pages left by implementing a while loop. Specificall ...

What is the best way to manage undefined status in react after the user chooses to cancel selecting a file?

Having an issue with my simple Input file type in React. I'm storing the selected file in state, but when I click the clear button, the state doesn't actually get cleared. This leads to {selectedFile.name} throwing an undefined error if the user ...

Guide on dynamically assigning the value of an Angular variable to another variable

My website has a slideshow feature with left and right buttons, similar to this example: . I am using Angular to change the image when the left or right button is clicked. In the function, I am incrementing a value: /*SlideShow Pictures*/ $scope.pic ...

Data loss in the array

I have a task where I need to slice 3 elements from an array and store them in another array array = [1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1]; rows = 3; Here is the method I am using getVertWallStruct = (array, rows) => { let i = 1, ...

I am encountering difficulties with a nodejs query where I am unable to successfully include the "+" symbol as part of the query

Every time I submit a query for B+ or A+ {{URL}}/api/help/?bloodType=B+ it ends up showing as empty space, like this. Is there a way to properly pass the "+" sign in the query? Thanks. P.S: _ works fine. {"bloodType":"B "} ...

PHP issues caused by Ajax form compatibility

I'm currently working on developing an upload website and I've encountered some challenges while trying to implement an upload progress bar. The Ajax form in my scripts seems to be causing issues with the PHP code, preventing the file from being ...

Determine who the creator of the clicked div is

I'm sorry if my explanation is a bit difficult to comprehend. Here is the code snippet in question: names.push(newName); var strName = newName; var newName = document.createElement('p') newName.setAttribute('id', newName); documen ...

Upon invoking useEffect, the information is displayed, only to encounter an error when attempting to access

After fetching data from a 3rd party API and logging it in the console, I encounter the error message Uncaught TypeError: Cannot read properties of undefined (reading 'tweet_results'). Despite having conditions in place, the data does not display ...

Tips on altering the location path when redirecting to a different page using window.location?

Is it a silly question to ask? I tried looking for the answer on Google but couldn't find it. I'm currently working on a website where users can upload photos or videos, using HTML, CSS, and JavaScript. On the homepage, when a user clicks on a ...

Adjusting the Pace of a CSS Marquee

My CSS marquee effect is working perfectly, but I am having trouble with the speed. The issue arises when the text length varies - shorter text takes longer to scroll while longer text scrolls quickly. This inconsistency is due to the animation duration an ...

Check for available usernames in real-time using Node.js and Express.js, along with client-side JavaScript

Currently, I am working on a web application using Express, MongoDB, and Handlebars as the templating engine. In this app, there is a form where users can create their unique usernames. I want to implement a feature where a tooltip pops up at intervals to ...

Issue with UseState causing a re-render not to be triggered

My orders list state works perfectly on the initial update. However, when an order in the database gets updated, the order list is updated but fails to trigger a re-render even with <...> const [orders, setOrders] = useState([]); useEffect(( ...

`How can I enable the download attribute feature on Safari browser?`

Is there a workaround for saving files with a specified name in Safari? The following HTML code does not work properly in Safari, as it saves the file as 'unknown' without an extension name. <a href="data:application/csv;charset=utf-8,Col1%2C ...

issues with jquery on function malfunctioning

I have some jQuery code that I'm struggling with $(".delete").on('click', function (e){ e.preventDefault(); var id = $(this).prop('id'); console.log('Delete button id: '+id); var formWrapper = $(".form-wr ...