HTML5之window.postMessage实现跨域传递消息

在做项目时,遇到了操作iframe的相关问题。业务很简单,其实就是在操作iframe内部某个窗体时,调用父窗体的一个函数。于是就写了两个很简单的htm页面用来测试,使用网上流行的方法在谷歌浏览器中始终报错,不能通过。

父页面parent.html的代码如下

[html]  view plain copy
  1. <html xmlns="http://www.w3.org/1999/xhtml">  
  2. <head><title>   
  3. </title>  
  4. <script src="jquery-1.10.1.min.js" type="text/javascript"></script>   
  5. <script type="text/javascript">       
  6.         function    ParentFunction() {  
  7.            alert('ParentFunction');          
  8.         }  
  9.      
  10.     </script></head>  
  11. <body>  
  12.  <input type="button" id="btnCancel" class="button" value="测试"  />   
  13.  <iframe id="FRMdetail"  name="FRMdetail" frameborder="0"  src='child.html' style="width:100%;height:100%;" ></iframe>  
  14. </body>  
  15. </html>  


子页面child.html的代码如下

[html]  view plain copy
  1. <html xmlns="http://www.w3.org/1999/xhtml">  
  2. <head><title>   
  3. </title>  
  4. <script src="jquery-1.10.1.min.js" type="text/javascript"></script>   
  5.  <script type="text/javascript">  
  6.         $(document).ready(function () {  
  7.             $("#btnTest").click(function (e) {        
  8.               var t=window.parent;             
  9.                  t.ParentFunction();  
  10.             });  
  11.         })    
  12.     </script></head>  
  13. <body>  
  14.  <input type="button" id="btnTest" class="button" value="应该获取的值"  />rrr  
  15. </body>  
  16. </html>  


网络上流行的方法 var t=window.parent; t.ParentFunction();在IE中能调用,可是在谷歌浏览器中总是提示如下错误,

Blocked a frame with origin "null" from accessing a frame with origin "null". Protocols, domains, and ports must match.

网上找了很长时间都没法发现方法,有的也是很早以前的版本,基本上没用了,而且人云亦云,基本上没有测试过。于是自己摸索,后来才发现,谷歌浏览器其实那种方法其实也可以,只是很奇怪,必须发布后才可以,在文件系统中调用,就会出现上边的错误。


其实还有一种html5的方法postMessage,于是就根据着进行了改写,最终代码如下:


父页面parent.html的代码如下

[html]  view plain copy
  1. <html xmlns="http://www.w3.org/1999/xhtml">  
  2. <head><title>   
  3. </title>  
  4. <script src="jquery-1.10.1.min.js" type="text/javascript"></script>   
  5. <script type="text/javascript">   
  6.        this.ParentFunctionfunction() {//和注释掉的方法是一样的,也就是说加不加this都是一样的,因为此处的this就是windows  
  7.            alert('ParentFunction');          
  8.         }  
  9.  //     function    ParentFunction() {  
  10.   //         alert('ParentFunction');          
  11.  //       }   
  12.      function receiveMessage(e) {  
  13.         var data = e.data;   //获得传递的值 
  14.         ParentFunction() ; 
  15.          //if(data=="ParentFunction")  
  16.          //{  
  17.          //  ParentFunction() ;  
  18.          //}         
  19.       }  
  20.       if (typeof window.addEventListener != 'undefined') {//使用html5 的postMessage必须处理的  
  21.       window.addEventListener('message', receiveMessage, false);  
  22.    } else if (typeof window.attachEvent != 'undefined') {  
  23.      window.attachEvent('onmessage', receiveMessage);  
  24.   }    
  25.     </script></head>  
  26. <body>  
  27.  <input type="button" id="btnCancel" class="button" value="测试"  />   
  28.  <iframe id="FRMdetail"  name="FRMdetail" frameborder="0"  src='child.html' style="width:100%;height:100%;" ></iframe>  
  29. </body>  
  30. </html>  

子页面child.html的代码如下

[html]  view plain copy
  1. <html xmlns="http://www.w3.org/1999/xhtml">  
  2. <head><title>   
  3. </title>  
  4. <script src="jquery-1.10.1.min.js" type="text/javascript"></script>   
  5.  <script type="text/javascript">  
  6.         $(document).ready(function () {  
  7.             $("#btnTest").click(function (e) {        
  8.               var t=window.parent;  
  9.               if(!t.ParentFunction)//在不支持时,使用html5 的postMessage方法 ,第一个参数是需要传递的值
  10.               {            
  11.                 t.postMessage("ParentFunction", '*');               
  12.               }  
  13.               else  
  14.               {   
  15.                  t.ParentFunction();                
  16.               }  
  17.             });  
  18.         })    
  19.     </script></head>  
  20. <body>  
  21.  <input type="button" id="btnTest" class="button" value="应该获取的值"  />rrr  
  22. </body>  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值