marcnyc marcnyc - 6 months ago 37
Javascript Question

how to target a frame of a frameset with an onload event

I have a frameset like this:

<frameset rows="120px,100%">
<frame name="top" id="top" src="top.php" noresize frameborder="0" scrolling="no" />
<frame name="main" id="main" src="main.php" noresize frameborder="0" scrolling="yes" />




Inside the main.php file I have:

<script>
function reload_top() {
top.frames['top'].location.href= "http://www.google.com";
}
</script>


And the onload event of the main.php body is:

<body onload="reload_top();">


What I would like to achieve is that every time somebody loads main.php that file re-loads the top frame top.php inside the top from, but so far I can only get it to break out of the frameset and load a new page...

I've tried any one of these:

document.getElementById('top').src = "http://www.google.com";

window.top.location.href = "http://www.google.com";

top.frames['top'].location.href= "http://www.google.com";


None of them work.
What am I doing wrong?

Thanks

Answer

As an information, frames tag is no more supported in HTML5. Would you want to use iframe to load different pages in one page.?

The problem you are unable to set the location of frame is because it is undefined when you try to access the frame

e.g. code

<iframe id="topf" src="stacktop.php"></iframe>
<script>
     function reload_top() {
        var a = document.getElementById('topf');
         a.src = "http://www.aapastech.com";
    }
 </script>