Thursday, 11 May 2017

Javascript Electron Cannot run function between index.html and index.js

I am having issues with an Electron project where I have 2 pages.

index.html and index.js.

On index.html I have an onclick that calls a function which is on index.js but it will not run it.

Here is the code:

INDEX.HTML

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title></title>
</head>
<body>

<button onclick="runfunction();">click me</button>

<script src="./index.js" type="application/javascript"></script>
</body>
</html>

INDEX.JS

const electron = require('electron')
const remote = require('electron').remote
const fs = require('fs')
const app = electron.app
const BrowserWindow = electron.BrowserWindow

const path = require('path')
const url = require('url')

let mainWindow

function createWindow () {

  mainWindow = new BrowserWindow({width: 800, height: 600})

  mainWindow.loadURL(url.format({
    pathname: path.join(__dirname, 'index.html'),
    protocol: 'file:',
    slashes: true
  }))

  mainWindow.webContents.openDevTools()

  mainWindow.on('closed', function () {
    mainWindow = null
  })
}

app.on('ready', createWindow)


app.on('window-all-closed', function () {
  if (process.platform !== 'darwin') {
    app.quit()
  }
})

app.on('activate', function () {
  if (mainWindow === null) {
    createWindow()
  }
})


}

//Function here:

function runfunction() {
    //do something here
}

The function above does not run. Not errors, just nothing happens.

I'm I forgetting something or doing this the wrong what?

How can I fix this?



via dj2017

No comments:

Post a Comment