框架中页面之间通信

dom07.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
<script type="text/javascript">
function test(){
alert("this is a function in parent")
}
function testCall(){
//第一种 获取iframe的方式 通过数组下标获取
//console.log(window.frames[0]);
//当有名字的时候 可以通过对象的方式去得到
//console.log(window.frames.iframe1);

//通过window直接获取 window <==> window.frames
//console.log(window[0]);
//console.log(window['iframe1']);
//console.log(window.iframe1);
var a = window[0].iframe1();
alert(a);
}
var win;
function testOpen(){
win = window.open("iframe2.html","","width=300px,height=400px");
//console.log(a);

}

function testCall1(){
win.iframe2();
}
</script>
<style>
iframe{
400px;
height: 300px;
border: 1px solid;
}
</style>
</head>
<body>
<input type="button" value="click" onclick="testCall()">
<input type="button" value="open" onclick="testOpen()">
<input type="button" value="call" onclick="testCall1()">
<hr>
<iframe src="iframe1.html" name="iframe1" frameborder="0"></iframe>
<iframe src="iframe2.html" name="iframe2" frameborder="0"></iframe>
</body>
</html>


iframe1.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
<script>
function iframe1(){
alert("iframe1");
return 123;
}

function testCall(){
//console.log(window);
window.parent.test();
}
</script>

</head>
<body>
<input type="button" value="click" onclick="testCall()">
<h1>this is a iframe 1</h1>
</body>
</html>


iframe2.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
<script>
function iframe2(){
alert("iframe2");
}

function testCall(){
window.opener.test();
}
</script>
</head>
<body>
<input type="button" value="click" onclick="testCall()">
<h1>this is a iframe 2</h1>

</body>
</html>

原文地址:https://www.cnblogs.com/hwgok/p/5739818.html