将变量传递给新打开的窗口 - nwj(node-webkit)

时间:2015-11-19 19:18:46

标签: javascript variables parameters node-webkit window-object

我正在搜索几个小时,关于如何使用node-webkit将变量传递给新打开的窗口。

这个任务非常简单(比如HTTP POST),但在nwj(node-webkit)文档中没有任何关于此的内容。

我用来打开新窗口的代码:

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;
});

谢谢!

2 个答案:

答案 0 :(得分:2)

您可以使用" emit"用于将数据传递到新窗口的函数。您需要做的就是拦截此事件,您可以从您传入的对象中提取参数。

例如,在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>

然后在你的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>

答案 1 :(得分:0)

根据当前的nwjs文档,更新后的代码如下:

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>...