1. 程式人生 > 其它 >9、electron子視窗向父視窗傳遞引數

9、electron子視窗向父視窗傳遞引數

主要用到了js 裡面的 window.opener.postMessage();  在html頁可以直接執行,跟electron沒有直接關係

1、新建父視窗頁“06父視窗接收子視窗.html”

 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <meta http-equiv="X-UA-Compatible" content="IE=edge">
 6     <meta name="viewport" content
="width=device-width, initial-scale=1.0"> 7 <title>Document</title> 8 </head> 9 <body> 10 <input type="button" id="btn" value="開啟子視窗" /> 11 <span id="txt"></span> 12 <script> 13 let btn = document.getElementById("btn"); 14 btn.onclick
= (e)=>{ 15 window.open('./06子視窗向父視窗傳引數.html'); 16 } 17 18 window.addEventListener('message',(msg)=>{ //監聽子視窗傳遞過來的引數 19 let txt = document.getElementById("txt"); 20 //txt.innerHTML = JSON.stringify(msg); 21 txt.innerHTML = msg.data; //展示傳遞過來的資訊 22 }) 23
</script> 24 </body> 25 </html>
View Code

2、新建子視窗頁“06子視窗向父視窗傳引數.html”

 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <meta http-equiv="X-UA-Compatible" content="IE=edge">
 6     <meta name="viewport" content="width=device-width, initial-scale=1.0">
 7     <title>Document</title>
 8 </head>
 9 <body>
10     <h1>子視窗用來向父視窗傳遞引數</h1>
11     <input type="button" id="btn" value="向父視窗傳遞資訊" />
12     <script>
13         let btn = document.getElementById("btn");
14         btn.onclick = (e)=>{
15             window.opener.postMessage('子視窗傳遞過來的引數');   //傳遞引數
16         }
17     </script>
18 </body>
19 </html>
View Code

3、執行命令

.\node_modules\.bin\electron .

4、執行效果: