views:

37

answers:

2

Hi,

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 is though, when I set the parent.document.getElementById("iframe1").src to url, which works for the first url but it doesn't seem to cycle through to the next url.

If though 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.

Not sure if it's 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.docuement?

I am using ie6

Thanks.

+1  A: 

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>
prem
A: 

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.

quantumSoup