Sir Rubberduck
Sir Rubberduck

Reputation: 2252

Electron: How to dynamically create windows?

Is there a way to create any window with one function, and be able to identify it?

let newWindow;

function createNewWindow(window) {
        newWindow = new BrowserWindow({width: 600, height: 400});
        newWindow.loadURL(`file://${__dirname}/windows/${window}.html`)
    }
}

createNewWindow("window1"); // Loads window1.html
createNewWindow("window2"); // Loads window2.html

The problem now is that both windows are created as newWindow, and calling newWindow.focus() for example, focuses the second window.

Upvotes: 0

Views: 826

Answers (1)

ztadic91
ztadic91

Reputation: 2804

Because you are sharing the variable for both windows. After the second createNewWindow('window2') call your newWindow points to the second BrowserWindow. Try using different variables.

function createNewWindow(window) { 
        const newWindow = new BrowserWindow({width: 600, height: 400});
        newWindow.loadURL(`file://${__dirname}/windows/${window}.html`)
        return newWindow;
    }
}

let secondWindow = createNewWindow("window2");

Upvotes: 2

Related Questions