Open new Window in NW.js without menubar

3.2k views Asked by At

Hi I have an app using NW.JS and angularjs. But the problem is, I want to open a new window (that loads a new page from within the app) without inheriting the menubar from the main window's menu. Is this possible in nwjs? Also every time I click the menu item from main window menubar that triggers the opening of new window it always open new window. How can I prevent this? I mean, when I already open the new window, the app should refrain from opening it again unless it was already closed.

in my index.html

var menu = new nw.Menu({ type: 'menubar' });

var submenu = new nw.Menu();
submenu.append(new nw.MenuItem({
    label: 'Exit',
    click: function(){
        nw.App.quit();
    }
}));

menu.append(new nw.MenuItem({
    label: 'File',
    submenu: submenu
}));
menu.append(new nw.MenuItem({
    label: 'New Page',
    click: function(){
        nw.Window.open('index.html#/new_page')
    }
}));

nw.Window.get().menu = menu;
1

There are 1 answers

4
Saket Patel On BEST ANSWER

When opening new window you are using same html file which contains code to create menu bar, so it is obviously going to create menu bar for popup window also, you need to use different html file for popup window.

Below is the full version of code, let me know if it is not working for you

index.html

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" /> 
    <title>Demo</title>

    <script type="text/javascript">
        //var win = nw.Window.get();
        //win.showDevTools();

        var menu = new nw.Menu({ type: 'menubar' });

        var submenu = new nw.Menu();
        submenu.append(new nw.MenuItem({
            label: 'Exit',
            click: function(){
                nw.App.quit();
            }
        }));

        menu.append(new nw.MenuItem({
            label: 'File',
            submenu: submenu
        }));
        menu.append(new nw.MenuItem({
            label: 'New Page',
            click: function(){
                console.log('open new page');
                var parentWin = window;

                if(parentWin.localStorage.getItem('child_open')) {
                    console.log('child window is already open');
                    return;
                }

                nw.Window.open('home.html#new_page', {}, function(win) {
                    parentWin.localStorage.setItem('child_open', true);

                    win.on('closed', function() {
                        parentWin.localStorage.removeItem('child_open');
                    });
                });
            }
        }));

        nw.Window.get().menu = menu;
    </script>
</head>
<body>
</body>
</html>

home.html

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" /> 
    <title>Inner Window</title>
</head>
<body>
    <br /><br /><br /><br /><br /><br />
    <br /><br /><br /><br /><br /><br />
    <br /><br /><br /><br /><br /><br />
    <br /><br /><br /><br /><br /><br />
    <p id="new_page"> New page section </p>
</body>
</html>