In this answer it is suggested that a javascript function can be defined and attached to window
in the preload.js
file and then called from the renderer process (either in included renderer.js
or directly as a script in the html file) using window.functionName
, i.e. in preload.js
:
window.myFunction = function(){
//do something here that requires
//ipcRenderer
}
and in index.html
:
<script>
var myButton = document.getElementById("myButtonId")
myButton.addEventListener('click', (e) => {
window.myFunction();
});
</script>
However, when I do it like this and click the button, I get the error message
Uncaught TypeError: window.myFunction is not a function
.
Can someone explain to me why this error is thrown and how to define the function instead?