So I have a function in main file main.js which creates Electron BrowserWindow. Lets say:
function HelloWorld(name){
return 'Hello World! said ' + name;
}
Can I call it in the html page loaded by Electron?
<html>
<head>
<script type="text/javascript">
const hello = require('electron').HelloWorld
</script>
</head>
<body onLoad="alert(hello);">
</body>
</html>
Can I do that?
Yes you can.
In your main process (probably main.js) put this line in your main process :
global.HelloWorld = function(name){
return 'Hello World! said ' + name;
}
and in your HTML :
<html>
<head>
<script type="text/javascript">
let {remote} = require('electron');
const hello = remote.getGlobal("HelloWorld")(); // <-- () this is important
</script>
</head>
<body onLoad="alert(hello);">
</body>
</html>
But I suggest use ipcMain
and ipcRenderer
to send data between process.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.