So I have an application where I create new window on button click. And now I would like to add some event listener on that new window (to write something in console), but it just does not work. How can I trigger that event listener on a new window, after new window is created on button click?
My code from one of js files:
const { BrowserWindow } = require('@electron/remote')
const remote = require('@electron/remote/main')
//Creating new window
let secondWindow = new BrowserWindow({
kiosk:false,
frame:true,
show:false,
})
// On mouse click on main window show new window (secondWindow)
window.addEventListener('mousedown', function(event) {
secondWindow.show();
secondWindow.focus();
secondWindow.webContents.openDevTools()
})
// And now i can't do this:
secondWindow.addEventListener('mousedown', function(event) {
console.log('Second window click')
})
As per the documentation, you should make sure that the BrowserWindow is ready before you attach the event listener to it.
If the BrowserWindow is not ready you will be trying to attach an event to an undefined container.
Check the docs for more infos
const { BrowserWindow } = require('electron')
const win = new BrowserWindow({ show: false })
win.once('ready-to-show', () => {
win.show()
// attach your event here
})