Search code examples
javascriptvariablesparametersnode-webkitwindow-object

Passing variables to a newly opened window - nwj (node-webkit)


I am searching for hours now, on how to pass a variable to a newly opened window using node-webkit.

The task is pretty simple (like HTTP POST), but there is no word about this, in the nwj (node-webkit) documentation.

code I use to open new windows:

var win = gui.Window.open ('print.html', {
  position: 'center',
  width: 901,
  height: 127
});
win.on ('loaded', function(){
  // the native onload event has just occurred
  var document = win.window.document;
});

Thanks!


Solution

  • You can use the "emit" function to pass data to a new window. All you then need to do is to intercept this event and you can extract your parameters from the object you've passed in.

    For example, in your index.html:

    <html>
     <head>
      <script type="text/javascript">
      window.gui = require('nw.gui');
      var win = gui.Window.open ('print.html', {
      position: 'center',
      width: 901,
      height: 127
      });
    
      win.on ('loaded', function(){
        var parameters = {greeting: "Hello World"}
        win.emit("data", parameters);
      });
       </script>
     </head>
     <body>
     </body>
    </html>
    

    And then in your print.html:

    <html>
     <head>
      <script type="text/javascript">
        var gui = require('nw.gui');
        var win = gui.Window.get();
        win.on("data", function(data) {
            document.write(data.greeting);
        });  
      </script>
     </head>
     <body>
     </body>
    </html>