0

HTML ページに 2 レベルの親子 iframe 階層があります。何らかの操作のために、親ウィンドウ ドキュメントのオブジェクトを子ドキュメントに取得したいと考えています。主に Google Chrome を使用しています。

parent.document は、Google Chrome では「未定義」になりますが、Mozilla では問題なく動作します。キャッチは何ですか?

参考までに、問題を示す 3 つのファイルの内容を以下に示します。

最初のファイル: 'one.html'

<html>
<head>
</head>
<body>
    <input type="hidden" id="one_1" name="one_1" />
    <iframe id="one" name="one" src="two.html">
</body>
</html>

2 番目のファイル:「two.html」

<html>
<head>
</head>
<body>
    <input type="hidden" id="two_1" name="two_1" />
    <iframe id="two" name="two" src="three.html">
</body>
</html>

3 番目のファイル:「three.html」

<html>
<head>
<script type="text/javascript">

    function callme() {
        alert(parent.document)
        alert(top.document)
    }
</script>
</head>
<body>
    <input type="hidden" id="three_1" name="three_1" />
    <button id="click" name="click" onclick="return callme()">Click Me</button>
</body>
</html>

「one.html」が Google Chrome で開かれていると仮定すると、「クリックしてください」ボタンをクリックすると、2 つの連続する警告ボックスが「未定義」の値で表示されます。Mozilla で「one.html」を開くと、2 つの「objectHTMLDocument」値の警告ボックスが表示されます。

[Click Me] ボタンをクリックしたときのコンソール メッセージの下に表示されます。

Unsafe JavaScript attempt to access frame with URL file:///C:/Users/user/Desktop/two.html from frame with URL file:///C:/Users/user/Desktop/three.html. Domains, protocols and ports must match.
                                                                 three.html:6
callme                                                           three.html:6
onclick                                                         three.html:13

Unsafe JavaScript attempt to access frame with URL file:///C:/Users/user/Desktop/one.html from frame with URL file:///C:/Users/user/Desktop/three.html. Domains, protocols and ports must match.
                                                                 three.html:7
callme                                                           three.html:7
onclick                                                         three.html:13

前もって感謝します。

4

1 に答える 1

0

ボトムアップにアクセスするのではなく、トップダウンから注入します。変数を選択して変数に保存することで、iFrame に変数を設定します。

var frame = document.getElementById('one');

そして、親への参照を挿入します。

frame.contentWindow.frame_parent_reference = window;

次に、次の子 iFrame でこれを実行し、「one」を「two」に置き換えます。このように、third.html によって、親またはトップを要求しませんが、次のことができます。

alert(frame_parent_reference.document);
alert(frame_parent_reference.frame_parent_reference.document);

あまり洗練されていないかもしれませんが、確実に多くの制御が可能になります (また、セキュリティのためにカスタム参照が存在するかどうかを確認できます)。

幸運を!

于 2013-03-26T21:24:58.613 に答える