Binding of a button click event in the Electron element

I started using electronic js to develop a desktop application.

I want to know how to associate a button click event with a javascript function so that I can perform another operation.

I used the below HTML code:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>Manav Finance</title>
  </head>
  <body>
    <input type="button" onclick="getData()" value="Get Data" />
  </body>

  <script>
    // You can also require other files to run in this process
    require('./renderer.js')
  </script>
</html>

My renderer.js code is as follows:

function getData(){
        console.log('Called!!!');
    }

But I get an error message:

Untrained ReferenceError: getData not defined in HTMLInputElement.onclick

Am I doing something wrong?

Update

Updated the HTML document and removed the require () method and now it works:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>Manav Finance</title>
    <script src="renderer.js"></script>
  </head>
  <body>
    <input type="button" id="btnEd" value="Get Data" onclick="getData()" />
  </body>
</html>
+10
source share
1 answer

. <script> HTML , , this === window, .. , , .

require , ( , this !== window, .. , , .

- require('./renderer.js')

function getData() {
    ...
}

document.querySelector('#btnEd').addEventListener('click', () => {
    getData()
})
+16

Source: https://habr.com/ru/post/1016146/


All Articles