0

シナリオ:

  1. domain1.com は domain2.com の iFrame をホストします。

  2. domain2.com は、javascript を使用して、親ウィンドウにルーティングする必要がある動的リンクをトリガーしています。

  3. リンクはすべて相対パスです。

  4. /linkA.html (domain2.com の iframe 内) をクリックすると、親が domain1.com/linkA.html にルーティングされます。

    var _generic = "/linkA.html";

        $("#canvas").each( function () {
            $(this).children("a").attr("href",_generic);
            $(this).click( function(e) {
                e.preventDefault();
                window.parent.location.href = _generic;
            } );
        } );
    

リンクを絶対 (domain2.com/linkA.html) に変更すると、機能上の問題が解決します。

誰もこれに遭遇したことがありますか?

4

4 に答える 4

1

相対パスを設定するときにブラウザーが URL 全体を解決するには、まず現在の href を読み取る必要がありますが、これは SOP によってブロックされます。

parent.locationしかし、代わりにを使用すると、これは問題なく機能すると確信していますparent.location.href

于 2011-07-13T19:25:41.470 に答える
1

window.opener.locationの代わりに使用してみてくださいwindow.parent.location

于 2011-07-13T19:59:58.267 に答える
0

IE9 と IE8、FireFox、および Webkit の違いを見てきました。次のコードを検討してください。

親ソース:

<!DOCTYPE html>
<html>
<head><title>iframe test page</title></head>
<body>
    <h1>Parent window</h1>
    <iframe src="//domain2/iframe.html"></iframe>
</body>
</html>

iframe ソース (別のドメイン):

<!DOCTYPE html>
<html>
<head><title>iframe content</title></head>
<body>
    <h1>iframe content</h1>
    <script>function redirect() { window.parent.location = "/new-href"; } </script>
    <a href="javascript:redirect()">Redirect the parent window please</a>
</body>
</html>

メイン ページが IE8 (または IE8 モードまたは Chrome 23 または FF16 で実行するように設定された IE9) で開かれている場合、リンクをクリックすると //(iframe ドメイン)/new-href に移動しますが、IE9 では //(元の親ドメイン)/new-href

私はまだこの問題を回避する方法を調査しており、さらに情報があれば更新します。

更新: iframe リンクの target="_top" 属性を設定すると、この問題を回避できます。

更新された iframe ソース (別のドメイン上):

<!DOCTYPE html>
<html>
<head><title>iframe content</title></head>
<body>
    <h1>iframe content</h1>
    <a href="/new-href" target="_top">Redirect the parent window please</a>
</body>
</html>

この更新された iframe コードのリンクをクリックすると、親 (全体) ウィンドウが IE8 と IE9 の両方で //(iframe ドメイン)/new-href にリダイレクトされます (私の知る限り、すべてのブラウザーも同様です)。それがあなたのやりたいことですよね?

たとえば、iframe 内のすべてのタグを作成して、iframe がどこかに移動するのではなく、トップ/メイン/親ウィンドウがどこかに移動するようにしたい場合は、次のようなものを使用します。

$("iframe a").attr("target", "_top");

ターゲット属性のドキュメントも参照してください

于 2012-11-12T16:31:37.777 に答える