简体   繁体   English

电子存储javascript函数

[英]Electron storing javascript functions

I have created a new electron application. 我创建了一个新的电子应用程序。

It consists of the following files: 它由以下文件组成:

index.html
main.js
renderer.js

In the index.html I have added a button with an onclick event: 在index.html中,我添加了带有onclick事件的按钮:

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

And the function is just an alert for testing purposes: 该功能只是出于测试目的的警报:

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

My problem is that I've added the function code to both the renderer.js and to the bottom of main.js and when I click the button I see no alert. 我的问题是我已将功能代码添加到renderer.js和main.js的底部,当我单击按钮时,我看不到任何警报。

How can I get js functions to be called from the index.html? 我如何从index.html获取要调用的js函数?

Let the myFunction reside in a separate javascript file. 让myFunction驻留在单独的javascript文件中。 Let's say it's named "functions.js". 假设它的名称为“ functions.js”。 To use this function in the HTML file (index.html), simply add the Javascript code using tag in the following manner - 要在HTML文件(index.html)中使用此功能,只需按以下方式使用标签添加Javascript代码-

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

Now, we will need to attach an onclick listener to the button that is rendered using the HTML code. 现在,我们需要将onclick侦听器附加到使用HTML代码呈现的按钮上。

To do that, add the following in functions.js 为此,请在functions.js中添加以下内容

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM