【问题标题】:Create a button to execute a .bat or .exe创建一个按钮来执行 .bat 或 .exe
【发布时间】:2019-12-06 07:53:11
【问题描述】:

我是 Electron 和 JS 的新手。

我已经寻找一种解决方案来创建一个执行.bat 文件或.exe 文件的简单按钮。

我已经阅读了article 关于使用child_process 的内容。

但是,它没有说明如何将 var“链接”到我的按钮。

我的代码写在renderer.js

【问题讨论】:

    标签: javascript html node.js electron


    【解决方案1】:

    electron 使用 nodejs 运行,因此您可以执行以下操作:

    var execFile = require('child_process').execFile;
    
        var runExe = function(){
           execFile('<your-name>.exe', function(err, data) {  
                console.log(err)
                console.log(data.toString());                       
            });  
        }
    

    现在打电话 runExe() 使用你的按钮,你应该很高兴

    更多信息请看这里 node js reference

    所以发生的情况基本上是我们使用你已经说过的 nodejs child_process 运行指定的 exe 文件...希望有所帮助

    【讨论】:

    • 好的,我明白了。但是在html中,怎么调用函数呢?
    • 非常感谢您的回复:)
    • 您可以简单地使用基本的 onclick 事件,请参阅此链接以获取信息 w3schools.com/tags/ev_onclick.asp
    • 成功了,谢谢 Gunnar Thoreson
    【解决方案2】:

    好的,现在是我发布电子解决方案的工作:

    <!DOCTYPE html>
    <html>
      <head>
        <meta charset="UTF-8">
        <title>Hello World!</title>
      </head>
      <body>
        <h1>Hello World!</h1>
        <!-- All of the Node.js APIs are available in this renderer process. -->
        We are using Node.js <script>document.write(process.versions.node)</script>,
        Chromium <script>document.write(process.versions.chrome)</script>,
        and Electron <script>document.write(process.versions.electron)</script>.
    	  <h1> A simple Javascript created button </h1>
    
         <button onclick="function()">Firefox 1</button>
         <button onclick="firefox()">Firefox 2</button>
    
        <script>
    var execFile = require('child_process').execFile;
    
    function firefox(){
           execFile("C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe", function(err, data) {  
                console.log(err)
                console.log(data.toString());                       
            });  
        }
    
    		// You can also require other files to run in this process
          require('./renderer.js')
    	</script>
      </body>
    </html>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-04-20
      • 1970-01-01
      • 2014-06-27
      • 2017-04-09
      • 1970-01-01
      • 2023-03-24
      • 1970-01-01
      相关资源
      最近更新 更多