0

最近、iframe がその親にアクセスするという悪名高い問題が発生しました。より単純なhtmlでそれを模倣しようとしました。そこで、parent.htm、child.htm という 2 つのファイルを作成しました。ローカルで実行されたにもかかわらず、通常のエラーが発生しています。

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>
4

1 に答える 1

0

そのエラーを再現することはできませんが、loadChild関数には 2 つのエラーがあります。

  • myParentの代わりに参照しmyParentWindowます。

  • innerHtml「innerHTML」の代わりに参照します。

固定スクリプト:

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

上記の修正後でも、Firefox 14 でそのエラーを再現できません。スクリプトの警告やエラーが発生することなく、すべて正常に動作します。

于 2012-08-17T08:44:59.817 に答える