javascript代码实例教程-js操作iframe兼容各种浏览器

发布时间:2019-03-25 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了javascript代码实例教程-js操作iframe兼容各种浏览器脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
小宝典致力于为广大程序猿(媛)提供高品质的代码服务,请大家多多光顾小站,小宝典在此谢过。

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

父页面parent.htML的代码如下


[html]&nbsp; <html XMlns="http://www.w3.org/1999/xhtml"> 
<head><tITle>  
</title> 
<script src="jquery-1.10.1.min.js" tyPE="text/javascript"></script>  
<script type="text/javascript">      
        function    ParentFunction() { 
           alert(&#39;ParentFunction');         
        } 
    
    </script></head> 
<body> 
 <input type="button" id="BTnCancel" class="button" value="测试"  />  
 <iframe id="FRMdetail"  name="FRMdetail" frameborder="0"  src='child.html' style="width:100%;height:100%;" ></iframe> 
</body> 
</html> 

<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>
</title>
<script src="jquery-1.10.1.min.js" type="text/javascript"></script>
<script type="text/javascript">   
     function ParentFunction() {
           alert('ParentFunction');       
        }
  
    </script></head>
<body>
 <input type="button" id="btnCancel" class="button" value="测试"  />
 <iframe id="FRMdetail"  name="FRMdetail" frameborder="0"  src='child.html' style="width:100%;height:100%;" ></iframe>
</body>
</html>

 

子页面child.html的代码如下

 

[html]  <html xmlns="http://www.w3.org/1999/xhtml"> 
<head><title>  
</title> 
<script src="jquery-1.10.1.min.js" type="text/javascript"></script>  
 <script type="text/javascript"> 
        $(document).ready(function () { 
            $("#btntest").click(function (e) {       
              VAR t=window.parent;            
                 t.ParentFunction(); 
            }); 
        })   
    </script></head> 
<body> 
 <input type="button" id="btnTest" class="button" value="应该获取的值"  />rrr 
</body> 
</html> 

<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>
</title>
<script src="jquery-1.10.1.min.js" type="text/javascript"></script>
 <script type="text/javascript">
        $(document).ready(function () {
            $("#btnTest").click(function (e) {    
     var t=window.parent;   
        t.ParentFunction();
            });
        }) 
    </script></head>
<body>
 <input type="button" id="btnTest" class="button" value="应该获取的值"  />rrr
</body>
</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]  <html xmlns="http://www.w3.org/1999/xhtml"> 
<head><title>  
</title> 
<script src="jquery-1.10.1.min.js" type="text/javascript"></script>  
<script type="text/javascript">  
       this.ParentFunction= function() {//和注释掉的方法是一样的,也就是说加不加this都是一样的,因为此处的this就是windows 
           alert('ParentFunction');         
        } 
 //     function    ParentFunction() { 
  //         alert('ParentFunction');         
 //       }  
     function receiveMessage(e) { 
        var data = e.data;    
         if(data=="ParentFunction") 
         { 
           ParentFunction() ; 
         }        
      } 
      if (typeof window.addEventListener != 'undefined') {//使用html5 的postMessage必须处理的 
      window.addEventListener(';message', receiveMessage, false); 
   } else if (typeof window.attachEvent != 'undefined') { 
     window.attachEvent('onmessage', receiveMessage); 
  }   
    </script></head> 
<body> 
 <input type="button" id="btnCancel" class="button" value="测试"  />  
 <iframe id="FRMdetail"  name="FRMdetail" frameborder="0"  src='child.html' style="width:100%;height:100%;" ></iframe> 
</body> 
</html> 

<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>
</title>
<script src="jquery-1.10.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
    this.ParentFunction= function() {//和注释掉的方法是一样的,也就是说加不加this都是一样的,因为此处的this就是windows
           alert('ParentFunction');       
        }
 //    function ParentFunction() {
  //         alert('ParentFunction');       
 //       } 
  function receiveMessage(e) {
     var data = e.data;  
   if(data=="ParentFunction")
   {
     ParentFunction() ;
   }      
      }
      if (typeof window.addEventListener != 'undefined') {//使用html5 的postMessage必须处理的
      window.addEventListener('message', receiveMessage, false);
   } else if (typeof window.attachEvent != 'undefined') {
     window.attachEvent('onmessage', receiveMessage);
  } 
    </script></head>
<body>
 <input type="button" id="btnCancel" class="button" value="测试"  />
 <iframe id="FRMdetail"  name="FRMdetail" frameborder="0"  src='child.html' style="width:100%;height:100%;" ></iframe>
</body>
</html>
子页面child.html的代码如下

 

[html]
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head><title>  
</title> 
<script src="jquery-1.10.1.min.js" type="text/javascript"></script>  
 <script type="text/javascript"> 
        $(document).ready(function () { 
            $("#btnTest").click(function (e) {       
              var t=window.parent; 
              if(!t.ParentFunction)//在不支持时,使用html5 的postMessage方法 
              {           
                t.postMessage("ParentFunction", '*');              
              } 
              else 
              {  
                 t.ParentFunction();               
              } 
            }); 
        })   
    </script></head> 
<body> 
 <input type="button" id="btnTest" class="button" value="应该获取的值"  />rrr 
</body> 
</html> 

<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>
</title>
<script src="jquery-1.10.1.min.js" type="text/javascript"></script>
 <script type="text/javascript">
        $(document).ready(function () {
            $("#btnTest").click(function (e) {    
     var t=window.parent;
     if(!t.ParentFunction)//在不支持时,使用html5 的postMessage方法
     {   
       t.postMessage("ParentFunction", '*');    
     }
              else
     {
        t.ParentFunction();    
     }
            });
        }) 
    </script></head>
<body>
 <input type="button" id="btnTest" class="button" value="应该获取的值"  />rrr
</body>
</html>

 

觉得可用,就经常来吧! 脚本宝典 欢迎评论哦! js脚本,巧夺天工,精雕玉琢。小宝典献丑了!

脚本宝典总结

以上是脚本宝典为你收集整理的javascript代码实例教程-js操作iframe兼容各种浏览器全部内容,希望文章能够帮你解决javascript代码实例教程-js操作iframe兼容各种浏览器所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。