【问题标题】:Failed to call parent function from remote iframe无法从远程 iframe 调用父函数
【发布时间】:2012-12-23 19:04:04
【问题描述】:

父.html

    <html xmlns="http://www.w3.org/1999/xhtml">  
    <head>  
    <script   type="text/javascript">  
    function say() { 
            alert("parent.html------>I'm at parent.html"); 
    } 

    function callChild() 
    {  
           myFrame.window.say(); 
    }  
    </script>  
    </head>  

    <body>    
    <input   type=button   value=" call child.html say()" onclick="callChild()"> 
    <iframe name="myFrame" src="http://66.175.214.250/child.html"></iframe> 
    </body>  
   </html>

child.html

   <html xmlns="http://www.w3.org/1999/xhtml">
   <head>  
   <script type="text/javascript"> 

   function say()  
   {  
        alert("child.html--->I'm at child.html");  
   } 

   function callParent() { 
        window.top.say(); 
   } 
   </script>  
   </head>  
   <body>  
   <input   type=button   value="call parent.html say() "   onclick="callParent()">  
   </body>  
   </html>

当 parent.html 和 child.html 在同一个文件夹中时,这是在 localhost 中测试的。但是如果我将 child.html 上传到远程主机。有错误:父不能调用子函数,子不能调用父函数。

我不知道为什么。是 iframe 调用,它的父级应该是本地的,还是远程主机块中的某个东西?

谢谢

【问题讨论】:

  • 两个页面的 URL 必须具有相同的方案(“http”或“https”)、完全相同的域名和相同的端口号(如果指定)。

标签: javascript


【解决方案1】:

即使像从子框架中离开子域 (www.) 这样简单的事情也可能导致您的浏览器停止两个浏览器之间的任何和所有通信,因为它们不会被视为同一来源或域。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-01-10
    • 2012-03-30
    • 2011-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多