背景:两个ABhtml页面 A页面通过iframe嵌套B页面 两个页面之间进行数据接收或传递。
A.html
<body>
<button id="button">发送到B</button>
<iframe src="B.html" id='myframe'></iframe>
</body>
<script>
// A 向B传递信息
document.getElementById("button").contentWindow.postMessage("sendMessage", '*');
// A 接收B的信息
window.addEventListener("message", function(e){
if (e.data === 'reimbClose') {
// 事件
}
}, false);
B.html
<body>
<button id="button">发送A</button>
</body>
<script>
var button = document.getElementById('button');
// B 向A传递信息
button.addEventListener('click',function(){
window.parent.postMessage('reimbClose','*');
},false)
// B 接收A的信息
window.addEventListener('message', function(e) {
if(e.data === 'sendMessage'){//关闭窗口停止websocket
// 事件
}
})
Comments | NOTHING