dad.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
#frame{
width: 200px;
height: 200px;
background-color: rgba(0, 255, 0, 0.2);
}
</style>
</head>
<body>
<iframe id="frame" src="./son.html" frameborder="10"></iframe>
<script>
window.οnlοad=function(){
const frame = document.getElementById("frame").contentWindow;
frame.postMessage('hello son!',"*");
}
const sleep = async (time)=>new Promise((resolve,reject)=>{
setTimeout(resolve,time);
})
const main = async (func)=>{
await sleep(1000);
(typeof func==="function")&&func()
}
window.onmessage=function(e){
console.log(e.data);
main(()=>{
e.source.postMessage("hello son",'*')
});
}
</script>
</body>
</html>
son.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
div {
background: red;
border-radius: 50%;
width: 100px;
height: 100px;
}
</style>
</head>
<body>
<div id="app"></div>
<script>
window.onload = function () {
}
const sleep = async (time)=>new Promise((resolve,reject)=>{
setTimeout(resolve,time);
})
const main = async (func)=>{
await sleep(1000);
(typeof func==="function")&&func()
}
window.onmessage = function (e) {
console.log(e.data);
main(()=>{
e.source.postMessage("hello dad","*");
});
}
</script>
</body>
</html>
tips
如果还不清楚或者想交个朋友的同学可以微信联系我:qq981145483(备注:csdn)