Help - can't get the right frame reference

T

Tim Streater

I have this arrangement of frames:

<html><head><script type="text/javascript"></script></head>

<frameset rows="100,*">
<frameset cols="135,*,165">
<frame name="logo" src="some.html" scrolling="No">
<frame name="title" src="someother.html" scrolling="No">
<frame name="nav" src="yetmore.html" scrolling="No">
</frameset>
<frameset>
<frame name="mainpage" scrolling="No">
</frameset>
</frameset>
</html>

I want to load some html into the frame "mainpage". How do I refer to
this frame from within my javascript up there in the header? I've tried
a myriad of things like:

<script type="text/javascript">

window.frames["mainpage"].location.href = "evenmore.html";

</script>

but it always says "object needed". I can't quite see how to string
together the object reference I need and would appreciate a pointer.

Thanks,

-- tim
 
R

Random

Tim said:
I have this arrangement of frames:

<html><head><script type="text/javascript"></script></head>

<frameset rows="100,*">
<frameset cols="135,*,165">
<frame name="logo" src="some.html" scrolling="No">
<frame name="title" src="someother.html" scrolling="No">
<frame name="nav" src="yetmore.html" scrolling="No">
</frameset>
<frameset>
<frame name="mainpage" scrolling="No">
</frameset>
</frameset>
</html>

I want to load some html into the frame "mainpage". How do I refer to
this frame from within my javascript up there in the header? I've tried
a myriad of things like:

<script type="text/javascript">

window.frames["mainpage"].location.href = "evenmore.html";

</script>

but it always says "object needed". I can't quite see how to string
together the object reference I need and would appreciate a pointer.

Thanks,

-- tim


When you place JavaScript in the header, it is executed prior to the
page loading. In this case, you are attempting to access a frame which
doesn't yet exist, because it hasn't yet gotten to that section of the
HTML code.

Using an onload will wait until the document is loaded before
attempting to access an element, thereby avoiding the problem of
attempting to access an element before it exists.

window.onload = function () {
alert( window.frames["mainpage"].location.href );
}


Alternately, you may try this:
function testThis() {
alert( window.frames["mainpage"].location.href );
}


<frame name="mainpage" scrolling="No" onLoad=testThis()>

There are other ways to do it, but you get the idea.
 
V

VK

In IE model frameSet doesn't have "frames" collection anymore as well
as a bunch of other window properties. Other browsers did not get
broken enough yet I guess to take it out.
 
R

Random

VK said:
In IE model frameSet doesn't have "frames" collection anymore as well
as a bunch of other window properties. Other browsers did not get
broken enough yet I guess to take it out.

Which version of IE? The code above I tested in IE 6.0.2800.1106, SP1.
 
V

VK

In IE model frameSet doesn't have "frames" collection anymore

my bs ^ 2 (bs in square)
The only weak excuse that I did not use frames for several years by
now.

<html>
<head>
<title>Top Frameset</title>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1">
<script>

function test() {
//alert(self.document.title);
alert(self.frames['f2'].document.getElementsByTagName('P')[0].innerHTML);
}
window.onload=test;
</script>
</head>

<frameset cols="50%,*">
<frame name="f1" src="frame1.html">
<frame name="f2" src="frame2.html">
</frameset>

</html>
 
T

Tim Streater

Random said:
Tim said:
I have this arrangement of frames:

<html><head><script type="text/javascript"></script></head>

<frameset rows="100,*">
<frameset cols="135,*,165">
<frame name="logo" src="some.html" scrolling="No">
<frame name="title" src="someother.html" scrolling="No">
<frame name="nav" src="yetmore.html" scrolling="No">
</frameset>
<frameset>
<frame name="mainpage" scrolling="No">
</frameset>
</frameset>
</html>

I want to load some html into the frame "mainpage". How do I refer to
this frame from within my javascript up there in the header? I've tried
a myriad of things like:

<script type="text/javascript">

window.frames["mainpage"].location.href = "evenmore.html";

</script>

but it always says "object needed". I can't quite see how to string
together the object reference I need and would appreciate a pointer.

Thanks,

-- tim


When you place JavaScript in the header, it is executed prior to the
page loading. In this case, you are attempting to access a frame which
doesn't yet exist, because it hasn't yet gotten to that section of the
HTML code.

Using an onload will wait until the document is loaded before
attempting to access an element, thereby avoiding the problem of
attempting to access an element before it exists.

window.onload = function () {
alert( window.frames["mainpage"].location.href );
}


Alternately, you may try this:
function testThis() {
alert( window.frames["mainpage"].location.href );
}


<frame name="mainpage" scrolling="No" onLoad=testThis()>

There are other ways to do it, but you get the idea.

Thanks for this pointer. Putting the onload in the frame itself did not
seem to work (the onload was not triggered, I suppose because nothing
had been loaded) so I put it in the outer frameset and this works fine
on Safari 1.3, and recent IE and Netscape under XP.

So I suppose the lesson is that timing is everything.

-- tim
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top