Javascript 代码失去焦点

发布于 2024-09-07 18:24:23 字数 1089 浏览 2 评论 0原文

我正在使用以下代码:

<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").src 设置为 url 时,出现问题;它适用于第一个,但似乎无法循环到下一个。

但是,如果我在此 javascript 的相同上下文中创建一个 iframe,例如 iframe2,而只是使用:

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

而不使用 parent.document 调用,则一切正常。

当我发出 parent.document 调用时,它是否会失去 javascript 的焦点?

关于如何在调用 parent.document 时将焦点带回此 javascript 代码,有什么想法吗?

我用的是ie6。

I have the following code that I am playing with:

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

Basically it's a routine to cycle through urls in an array into an iframe. My problem occurs when I set parent.document.getElementById("iframe1").src to a url; it works for the first but it doesn't seem to cycle through to the next.

However, if I create an iframe in the same context of this javascript, say iframe2 and instead just use:

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

without the parent.document call, all works fine.

Is it losing the focus of the javascript when I issue the parent.document call?

Any ideas on how to bring focus back to this javascript code when calling a parent.document?

I am using ie6.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

七禾 2024-09-14 18:24:23

此代码更改应该有效。你需要给 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>

This code change should work. You need to give iframe a name and secondly, I didnt test it in IE6, but works in 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>
场罚期间 2024-09-14 18:24:23

您是否尝试过使用“top”而不是“parent”?

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

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

这样你有绝对的引用,不必担心它实际位于哪个窗口。

Have you tried using 'top' instead of 'parent'?

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

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

That way you have an absolute reference and don't have to worry about which window it actually is in.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文