访问iFrame Parent

时间:2012-08-16 03:28:17

标签: html iframe

我最近遇到了臭名昭着的问题,iframe访问其父级。我试图在更简单的htmls中模仿它。所以我创建了两个文件,parent.htm,child.htm。即使它们是在本地执行的,我也会遇到常见错误。

Unsafe JavaScript attempt to access frame with URL file:///C:/Users/kvaradar/Desktop/New%20folder/Parent.htm from frame with URL file:///C:/Users/kvaradar/Desktop/New%20folder/Child.htm. Domains, protocols and ports must match.

嗯,有人告诉父母和孩子必须在同一个域中。但就我而言,我将它们作为本地文件, - Parent.htm,Child.htm。

我假设他们应该在同一个域名上。为什么我在这种情况下会收到此错误?我错过了什么。

这是父HTML。

<html xmlns="http://www.w3.org/1999/xhtml">
<body>
<span id="myParentWindow">Some content of parent frame's span.</span>
<iframe src="Child.htm"></iframe>
</body>
</html>

儿童HTML。

<html xmlns="http://www.w3.org/1999/xhtml">

<script type="text/javascript">
    function loadChild() {
        document.getElementById('myChild').innerHTML
            = window.parent.document.getElementById('myParent').innerHtml;
    }
</script>
<body onload="loadChild()">
    <div style="border:1px solid green;height:500px; min-height:500px">

    Some content of the child frame.
    In the following span, I am trying to fill the parent's span content through javascript

    <span id="myChild"></span>
    </div>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

我无法重现该错误,但loadChild函数有2个错误:

  • 它引用myParent而不是myParentWindow

  • 它指的是innerHtml而不是`innerHTML'。

固定脚本:

function loadChild() {
  document.getElementById('myChild').innerHTML
    = window.parent.document.getElementById('myParentWindow').innerHTML;
}

即使经过上述修复,我也无法在Firefox 14中重复该错误。一切正常,没有任何脚本警告或错误。