【问题标题】:Passing variables to a newly opened window - nwj (node-webkit)将变量传递给新打开的窗口 - nwj (node-webkit)
【发布时间】:2016-02-22 02:34:19
【问题描述】:

我现在正在搜索几个小时,关于如何使用 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;
});

谢谢!

【问题讨论】:

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


    【解决方案1】:

    您可以使用“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>
    

    【讨论】:

    • 感谢您的帮助和详细的示例!
    • 当然——我也为此苦苦挣扎,偶然发现了解决方案。
    【解决方案2】:

    根据当前的 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>...
    

    【讨论】:

      猜你喜欢
      • 2013-02-05
      • 1970-01-01
      • 2013-11-16
      • 1970-01-01
      • 1970-01-01
      • 2016-12-21
      • 2017-06-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多