marcnyc marcnyc - 2 years ago 116
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:

function reload_top() {
top.frames['top'].location.href= "";

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 = ""; = "";

top.frames['top'].location.href= "";

None of them work.
What am I doing wrong?


Answer Source

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>
     function reload_top() {
        var a = document.getElementById('topf');
         a.src = "";
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download