<html lang="en-US"> <head> <title>Hello World!</title> <style> </style> </head> <body> <iframe src="http://rwdev.meilishuo.com/test/im" id="f"></iframe> <script > var win = document.getElementById('f').contentWindow var fs = require('fs') win.onload = function(){ var t = fs.statSync('./') win.postMessage(t,win.location.href) } win.addEventListener('message', function(event){ console.log('parent receive :' ,event.data) }, false) </script> </body> </html>
<html lang="en-US"> <head> </head> <body> <script > window.postMessage('t',window.location) window.addEventListener("message", function(event){ console.log('child receive' ,event.data) document.body.innerHTML += '<pre>'+ JSON.stringify(event.data) + '</pre>' }, false); </script> </body> </html>
跨窗口通信实践
本文介绍了一个使用HTML、JavaScript实现的跨窗口通信案例,通过iframe元素和postMessage API,展示了父窗体与子窗体间如何进行消息传递。文章还涉及了文件系统访问,以及如何在接收到消息时进行日志记录。
1321

被折叠的 条评论
为什么被折叠?



