3

私が遊んでいる次のコードがあります:

<script type="text/javascript">

var Dash = {
    nextIndex: 0,
    dashboards: [
        {url: 'http://www.google.com', time: 5},
        {url: 'http://www.yahoo.com', time: 10}
    ],

    display: function()
    {
        var dashboard = Dash.dashboards[Dash.nextIndex];
        parent.document.getElementById("iframe1").src = dashboard.url;
        Dash.nextIndex = (Dash.nextIndex + 1) % Dash.dashboards.length;
        setTimeout(Dash.display, dashboard.time * 1000);
    }
};

window.onload = Dash.display;

</script>

基本的には、配列内の URL を iframe に循環させるルーチンです。parent.document.getElementById("iframe1").srcURLを設定すると問題が発生します。最初は機能しますが、次のサイクルには循環していないようです。

ただし、この JavaScript の同じコンテキストで iframe を作成する場合は、iframe2 と言って、代わりに次を使用します。

        document.getElementById("iframe2").src = dashboard.url;

parent.document呼び出しがなければ、すべて正常に動作します。

parent.document呼び出しを発行すると、javascript のフォーカスが失われますか?

を呼び出すときに、この JavaScript コードにフォーカスを戻す方法についてのアイデアはありparent.documentますか?

私はie6を使用しています。

4

2 に答える 2

1

このコード変更は機能するはずです。iframeに名前を付ける必要があります。次に、IE6ではテストしませんでしたが、IE7では機能します。

<script type="text/javascript">

var Dash = {
    nextIndex: 0,
    dashboards: [
    {url: 'http://www.rediff.com', time: 5},
    {url: 'http://www.google.com', time: 10}
    ],

    display: function()
    {
    var dashboard = Dash.dashboards[Dash.nextIndex];
    parent.frames["fname"].location.href = dashboard.url;
        window.focus();
    Dash.nextIndex = (Dash.nextIndex + 1) % Dash.dashboards.length;
    setTimeout(Dash.display, dashboard.time * 1000);
    }
};

window.onload = Dash.display;

</script>
于 2010-06-30T01:58:40.757 に答える
0

「親」の代わりに「トップ」を使用してみましたか?

top.document.getElementById("iframe1").src = dashboard.url;

http://www.w3schools.com/jsref/prop_win_top.asp

そうすれば、絶対参照が得られ、実際にどのウィンドウにあるかを気にする必要がありません。

于 2010-06-30T02:00:27.990 に答える