cannot access links in an iframe

O

oscar1martin

The property document.links seems to access links inside a frame, but
not an iframe. For instance, the following code displays a "4": 2
links in main page, 2 links in frame1.


<body onload="fn()">
<script type="text/javascript">
function fn()
{
alert(document.links.length)
}
</script>

<a href="http://somewhere1">aaa</a>
<a href="http://somewhere2">bbb</a>

<frameset>
<frame name="frame1" id="frame1">
<a href="http://somewhere3">ccc</a>
<a href="http://somewhere4">ddd</a>
</frame>
<iframe name="frame2" id="frame2" src="http://somewhere5">
</iframe>
</frameset>

</body>


Is there a way to access the links in the iframe?

Thanks in advance.
 
L

-Lost

The property document.links seems to access links inside a frame, but
not an iframe. For instance, the following code displays a "4": 2
links in main page, 2 links in frame1.

<body onload="fn()">
<script type="text/javascript">
function fn()
{
alert(document.links.length)
}
</script>

It reports exactly what is found. Now, whether or not you understand
your markup is an entirely different thing.
<a href="http://somewhere1">aaa</a>
<a href="http://somewhere2">bbb</a>

<frameset>
<frame name="frame1" id="frame1">
<a href="http://somewhere3">ccc</a>
<a href="http://somewhere4">ddd</a>
</frame>
<iframe name="frame2" id="frame2" src="http://somewhere5">
</iframe>
</frameset>

</body>

How do you expect JavaScript to behave the way you want it to if you do
not provide it valid HTML?
Is there a way to access the links in the iframe?

Yes. You must first however embed it properly in a page. For example,
in a frame, in a frameset.
 
M

marsanos

Well, yes, I guessed I could be doing something wrong, but what is it?

I have skimmed trhough hundreds of examples on the net and they all
seem like mine to me.
Also, the page displays correctly.

Some more help, please?
 
M

marsanos

Now, this is a helpful answer. And a proof that there are a few things
I have to learn.

Thanks to both of you.
 

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,982
Messages
2,570,186
Members
46,739
Latest member
Clint8040

Latest Threads

Top