IFrame子窗口JS方法互相调用和传参

child1.html和child2.html都是父页面parent.html中的一个子iframe,请看child1.html如何调用child2.html中的方法。



--------------------------------------------------------------------------------
parent.html

view plaincopy to clipboardprint?
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<title>jQuery Cookie Plugin</title>
</head>
<body>
<iframe id="myFrame1" src="child1.html" mce_src="child1.html"></iframe>
<iframe id="myFrame2" src="child2.html" mce_src="child2.html"></iframe>
</body>
</html>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<title>jQuery Cookie Plugin</title>
</head>
<body>
<iframe id="myFrame1" src="child1.html" mce_src="child1.html"></iframe>
<iframe id="myFrame2" src="child2.html" mce_src="child2.html"></iframe>
</body>
</html>



--------------------------------------------------------------------------------


child1.html

view plaincopy to clipboardprint?
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<title>jQuery Cookie Plugin</title>
<mce:script type="text/javascript" src="jquery-1.4.2.js" mce_src="jquery-1.4.2.js"></mce:script>
<mce:script type="text/javascript"><!--
function test1() {
window.parent.document.getElementById("myFrame2").contentWindow.test2();
}

// --></mce:script>
</head>
<body>
<input type="button" οnclick="test1();" value="test" />
</body>
</html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<title>jQuery Cookie Plugin</title>
<mce:script type="text/javascript" src="jquery-1.4.2.js" mce_src="jquery-1.4.2.js"></mce:script>
<mce:script type="text/javascript"><!--
function test1() {
window.parent.document.getElementById("myFrame2").contentWindow.test2();
}

// --></mce:script>
</head>
<body>
<input type="button" οnclick="test1();" value="test" />
</body>
</html>


--------------------------------------------------------------------------------


child2.html

view plaincopy to clipboardprint?
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<title>jQuery Cookie Plugin</title>
<mce:script type="text/javascript" src="jquery-1.4.2.js" mce_src="jquery-1.4.2.js"></mce:script>
<mce:script type="text/javascript"><!--
function test2() {
alert("I am your brother");
}

// --></mce:script>
</head>
<body>
2
</body>
</html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<title>jQuery Cookie Plugin</title>
<mce:script type="text/javascript" src="jquery-1.4.2.js" mce_src="jquery-1.4.2.js"></mce:script>
<mce:script type="text/javascript"><!--
function test2() {
alert("I am your brother");
}

// --></mce:script>
</head>
<body>
2
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在父页面中调用页面的方法和变量,可以使用以下几种方式: 1. 使用iframe.contentWindow属性获取页面的window对象,然后调用该对象的方法或访问该对象的属性。 例如,假设iframe的id为"myFrame",页面中有一个名为"myFunction"的方法和一个名为"myVariable"的变量,可以使用以下代码来调用它们: ```javascript var iframe = document.getElementById("myFrame"); var childWindow = iframe.contentWindow; childWindow.myFunction(); var myValue = childWindow.myVariable; ``` 2. 在页面中定义一个全局变量或函数,并在父页面中通过iframe对象的contentWindow属性访问它们。 例如,页面中定义了一个名为"myFunction"的全局函数和一个名为"myVariable"的全局变量,可以使用以下代码在父页面中访问它们: ```javascript var iframe = document.getElementById("myFrame"); var childWindow = iframe.contentWindow; childWindow.myFunction(); var myValue = childWindow.myVariable; ``` 3. 在父页面中使用postMessage API向页面发送消息,并在页面中监听该消息,并根据消息内容执行相应的操作。 例如,在父页面中发送消息: ```javascript var iframe = document.getElementById("myFrame"); var childWindow = iframe.contentWindow; childWindow.postMessage("hello", "http://example.com"); ``` 在页面中监听消息: ```javascript window.addEventListener("message", function(event) { if (event.origin == "http://example.com" && event.data == "hello") { // 执行相应的操作 } }); ``` 需要注意的是,在使用postMessage API时需要注意安全性,防止被恶意代码利用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值