PDA

View Full Version : Referring to other frames in Javascript


Wilfredo
03-02-2006, 08:19 AM
'ello there

I have scoured the internet to find a solution to my problem but can't find anything remotely close, so I hoped someone here might be able to help...

I have a page with two frames. A main frame, and a smaller navigation frame. I am trying to code a button in Javascript on the navigation frame that brings up a new window displaying a list of all the links it found on the main frame.

The button uses the following Javascript...

function listLinks(doc){
var newWin = window.open();
for(var i = 0; i < doc.links.length; i++){
newWin.document.write("<A HREF = \"");
newWin.document.write(doc.links[i].href);
newWin.document.write("\">");
newWin.document.write(doc.links[i].innerText);
newWin.document.write("</A><BR>");
}
}

This works perfect if I pass down "document" to the function, so that it simply displays the list of links on the navigation frame, but I want it to display the links of the main frame instead, so I tried passing "top.content_pane" down to the funcion. Like so...

<button name="getlinks" onclick="listLinks(top.content_pane)";>

But I get the error message telling me "links" is null or undefined. I think it is having problems getting the array of links out of the main frame? Does anyone know a way around this at all?

Much much appreciated...

Will

Horus_Kol
03-02-2006, 11:28 AM
what is "links" - is it an object in the "content_pane" frame?

coothead
03-02-2006, 01:24 PM
Hi there Wilfredo,

and a warm welcome to these forums. :loopy:

Try this simple example, it may help....

frameset:-
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN"
"http://www.w3.org/TR/html4/frameset.dtd">
<html>
<head>
<title>A simple frameset document</title>
</head>
<frameset cols="20%, 80%">
<frame src="nav.html" >
<frame src="links.html">
</frameset>
</html>
nav.html:-
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>navigation</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<script type="text/javascript">
<!--
function listLinks(frame) {

var features='width=200,height=200,left=400,top=400,scrollbars=1';
var newWin = window.open('','',features);

for(i=0;i<frame.document.links.length; i++) {
newWin.document.write('<a href="'+frame.document.links[i].href+'">');
newWin.document.write(frame.document.links[i].innerHTML);
newWin.document.write('<\/a><br>');
}
newWin.document.close();
newWin.focus();
}
//-->
</script>

</head>
<body>

<div>
<button onclick="listLinks(parent.frames[1])">get links</button>
</div>

</body>
</html>
links.html:-
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>links</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

</head>
<body>

<div>
<a href="http://www.google.com">google</a>
<a href="http://www.htmlforums.com">htmlforums</a>
<a href="http://www.w3schools.com">w3schools</a>
</div>

</body>
</html>

Wilfredo
03-02-2006, 05:34 PM
Ah perfect, I see where I'm going wrong now!

Thanks a lot, I'll give it a go