最近、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>