Proper way to open new window


#1

Hello,
I am new to JavaScript and Electron and i have question for which i cannot find answer(or maybe i don’t know how to search). I have a simple button in HTML page:

< button type=“button”>Click Me!< /button>

How can i make it to open electron window?

I found this:

< a id=“demo” onclick=“myFunction()”>< button class=“btn btn-large btn-default moreInfo-Link-button”>More Info< /button>< /a>

                    <script>
                    function myFunction() {
                        const remote = require('electron').remote;
                    const BrowserWindow = remote.BrowserWindow;

                    var win = new BrowserWindow({ width: 800, height: 600 });
                      }
                    </script>

and it works, but i want to know what is recommended way of doing it?

Thanks.


#2

That’s more or less the recommended way. If you’re using it frequently though, it might be worth making BrowserWindow global (ie, set the variable outside of the function scope), but this works.

It may also be worth adding this, rather than showing it automatically, to avoid showing the flash of white when a new window opens:

win.webContents.on('did-finish-load', ()=>{
   win.show();
   win.focus();
 });
win.loadURL(myURL);