4

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!

Edmond Tamas
  • 3,148
  • 9
  • 44
  • 89

2 Answers2

3

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>
Ewald
  • 5,691
  • 2
  • 27
  • 31
0

Based on present nwjs documentation the updated code will be like:

index.html

...<script>let RVal
nw.Window.open('print.html', {},win=>win.on('loaded', () =>RVal=win.window.prnFunc(someData)))</script>...

print.html

...<script>function prnFunc(theData){alert(theData); return 4*theData/*or some such*/}</script>...
Chawathe Vipul S
  • 1,636
  • 15
  • 28