[ Front-end ] Iframe 跨域双向通信

本文介绍了一种使用Iframe实现父窗体与子窗体之间的双向通信方法,并提供了详细的代码示例。通过监听message事件,无论是父窗体向子窗体发送消息还是子窗体向父窗体发送消息,都能实现跨窗口的消息传递。
摘要由CSDN通过智能技术生成

Iframe 通信相信大家都接触过,或者了解过,那么双向通信呢? :p

今天发了Code Review ,有点时间就重新整理了一下,希望能对大家有所帮助。

0、效果图

1、ParentHtml

 1 <html xmlns="http://www.w3.org/1999/xhtml">
 2 <head runat="server">
 3     <title></title>
 4     <script src="Js/jquery-1.7.2.min.js" type="text/javascript"></script>
 5     <script type="text/javascript">
 6         function SendMessage() {
 7             var Msg = $("#txtParent").val();
 8             var Iframe = document.getElementById("iframe1");
 9             Iframe.contentWindow.postMessage(String(Msg), "*");
10         }
11     </script>
12     <script type="text/javascript">
13         var OnMessage = function (e) {
14             $("#lblParent").text(e.data);
15         };
16         function Init() {
17             // for > ie 8
18             if (window.addEventListener) {
19                 window.addEventListener("message", OnMessage, false);
20             }
21             // for <= ie 8
22             else {
23                 if (window.attachEvent) {
24                     window.attachEvent("onmessage", OnMessage);
25                 }
26                 else {
27                     alert(" can not init event listener.");
28                     return;
29                 }
30             }
31         };
32         Init();
33     </script>
34 </head>
35 <body>
36     <div>
37         <p>
38             接收到的信息:<label id="lblParent"></label>
39             <br />
40             <input type="text" id="txtParent" value="parent message" />
41             <input type="button" value="Send to Child" οnclick="SendMessage()" />
42         </p>
43         <p>
44             <iframe style="height: 400px;" id="iframe1" src="ChildHtml.aspx"></iframe>
45         </p>
46     </div>
47 </body>
48 </html>

2、ChildHtml

 1 <html xmlns="http://www.w3.org/1999/xhtml">
 2 <head id="Head1" runat="server">
 3     <title></title>
 4     <script src="Js/jquery-1.7.2.min.js" type="text/javascript"></script>
 5     <script type="text/javascript">
 6         function SendMessage() {
 7             var Msg = $("#txtChild").val();
 8             var Parent = window.parent;
 9             Parent.postMessage(String(Msg), "*");
10         };
11         var OnMessage = function (e) {
12             $("#lblChild").text(e.data);
13         };
14         function Init() {
15             // for > ie 8
16             if (window.addEventListener) {
17                 window.addEventListener("message", OnMessage, false);
18             }
19             // for <= ie 8
20             else {
21                 if (window.attachEvent) {
22                     window.attachEvent("onmessage", OnMessage);
23                 }
24                 else {
25                     alert(" can not init event listener.");
26                     return;
27                 }
28             }
29         };
30         Init();
31     </script>
32 </head>
33 <body>
34     <div>
35         <p>
36             接收到的信息:<label id="lblChild"></label>
37             <br />
38             <input type="text" id="txtChild" value="child message" />
39             <input type="button" value="Send to Parent" οnclick="SendMessage()" />
40         </p>
41     </div>
42 </body>
43 </html>

3、附上源码

Download

转载于:https://www.cnblogs.com/VincentDao/archive/2013/02/05/2892466.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值