Node-Webkit:nw.js 父子窗口间通信 Child window to call parent window

Node-Webkit:nw.js 父子窗口间通信 Child window to call parent window

先看效果

Node-Webkit:nw.js 父子窗口间通信 Child window to call parent window_第1张图片

感谢网友分享
参考文章http://www.360doc.com/content/16/0125/15/597197_530455358.shtml

父页面代码:index.html

<!DOCTYPE html>
<html lang="en">
    <head>
        <title>Hello World!</title>
        <meta charset="utf-8"/>
      </head>
      <body>
        <h1>Hello World!</h1>
        <h1>Very good,最简单的nw例子!</h1>
        <p>
          <button id="btn">重启</button>
          <button id="openSon">打开子页面</button>
          <button id="sendId">调用儿子</button>
        </p>
      </body>

      <script>
	  // window.location.href = 'http://localhost:8080/'
          var gui = nw.require('nw.gui');
          console.log('nw:',nw)
          console.log('gui:',gui)
          var win = nw.Window.get();
          var allSonArr = []
          // var win = gui.Window.get();
          console.log('win:',win);
          win.on('close',()=>{
     
              console.log('页面点击了关闭--');
            //   win.hide();
              setTimeout(()=>{
     
                //   win.show();
                win.onClose.dispatchNW()
                // win.reloadIgnoringCache()
              },3000)
              console.log('等待数据保存后,再关闭--');
          })

          let btn = document.getElementById('btn');
          btn.onclick = ()=>{
     
            let p = document.createElement('p');
              p.innerHTML ='正在重启,请稍后...'
              document.body.appendChild(p)
              setTimeout(()=>{
     
                //   win.show();
                // win.onClose.dispatchNW()
                win.reloadIgnoringCache()
              },3000)
          }
          
          win.on('closed',()=>{
     
              console.log('页面已经关闭--')
          })

          let openSon = document.getElementById('openSon');
          let sonPage;
          nw.Window.open('./otherPage.html',{
     
            x: 50,
            y:100
          },(sonWin)=>{
     
            sonWin.on('loaded',()=>{
     
              console.error('子页面加载完成--sonPage',sonWin);
              sonWin.window.sonFun(win,'我是父亲')
              // 存储儿子
              allSonArr.push({
     
                name: 'son1',
                obj: sonWin
              })
            })
          })
          
          function faFun(param){
     
            console.error('faFun',param);
          }

          let sendId = document.getElementById('sendId');
          sendId.onclick = ()=>{
     
            allSonArr[0].obj.window.send('FA:'+Date.now())
          }

          function send(param){
     
            console.error('fa-send:',param);
          }
      </script>
</html>

子页面代码:otherPage.html

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>子窗口</title>
</head>
<body>
  我是子窗口
  <button id="sendId">调用父亲</button>
  <script>
    var gui = require('nw.gui');
    var win = gui.Window.get();
    var faWin;
    function sonFun(faObj){
     
      console.error('son-fun',arguments);
      faObj.window.faFun('儿子调用的');
      // 存储父亲
      faWin = faObj;
    }

    function send(param){
     
      console.error('son-send:',param);
    }

    let sendId = document.getElementById('sendId');
    sendId.onclick=()=>{
     
      if(faWin){
     
        faWin.window.send('Son:'+Date.now())
      }
    }
  </script>
</body>
</html>

欢迎留评!

你可能感兴趣的:(nwjs,vue,js,js,nwjs,node)