Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

iframe confusion

Status
Not open for further replies.

plork123

Programmer
Mar 8, 2004
121
GB

I have 2 pages
main.asp
and searchresults.asp

On main.asp I have my code which inc. an iframe
<iframe width="350" height="265" name="searchResults" src="searchResults.asp">

On searchresults.asp i want to call one of the functions on main.asp in the body onload event
<body onload="........functiontobecalled()">

But I don't know how to reference the main.asp page. Can someone help

Thanks in advance
 


No worries, fiured it out, simple really

parent.functionname()
 

One more thing

On my parent page (main.asp) I have an iframe.

Within my script i want to refernce that iframe

i thought i could use something like this
top.searchResults, but this doesn't work

Cna anyone help
 
If "searchResults" is the name of the IFRAME, then try:

parent.frames["searchResults"]

...assuming call is coming from a different IFRAME on the same page, or:

document.frames["searchResults"]

...if call is coming from same page.

'hope that helps.

--Dave
 
Where is the iframe in relation to the JavaScript? At the same level? Did you set the IFRAME's NAME attribute to "searchResults" or its ID attribute? It should be the ID.

<iframe id="searchResults" ...>

--Dave
 

I've tried both ways through id="searchResults" and name="searchResults"

What I am trying to do is load main.asp and the user puts a number in the box. Clicks the button and function factorial is run, the results are then put in the iframe which is searchResults.asp

The code errors on var outputFrame = top.searchResults; because I am not able to access the iframe

This is my code. Your help will be much appreciated

Code:
<html>
<head>
	<script language="javascript">

		var outputFrame = top.searchResults;

		function onLoad()
		{
			outputFrame.document.getElementById("text").style.visibility = 'hidden';
			outputFrame.document.getElementById("text").value = "";
		}

		function factorial(n)
		{
			var tableWidth = 4;
			var lineIdx = 0;
			var outputStr = "";			
			outputStr += "<table cellpadding='7' border='1'><tr>" ;			
			outputStr += "<th colspan='"+tableWidth+"' align='center' >Factorial("+n+")</th></tr><tr>";			
			var f = 1;   
			
			for (var i=n; i>0; i--)
			{
				if(lineIdx == tableWidth )
				{
					outputStr +=  "\n\t</tr><tr>" ;
					lineIdx = 0;  
				}  
			
				f = f * i;
				outputStr += "\n\t\t<td>"+f+"</td>";
				lineIdx++;
			}
			
			outputStr +=  "\n</tr></table>";
			outputFrame.document.getElementById("table").innerHTML = outputStr;
			outputFrame.document.getElementById("text").value = outputStr;
		}						
	</script>

<body>

	<form name="f" action="">
     		<tr><td>Please enter an integer:</td>
                <td><input name = "number" type = "text" onblur="checkInt(this);" size="20"/></td>
                <td><input type = "button" value = "Calculate"onclick = "factorial(document.getElementById("number").value)"/></td>

		<div style="border-style:solid; border-width:1px; position:absolute; width:350px; height:265px; top:175; left:10; padding:1px;">
			<iframe width="350" height="265" name="searchResults" src="searchResults.asp" border="1">
		</div>
	
        </form>
</body>

</html>
</head>


searchResults.asp

<html>
<body onload="parent.onLoad()">
	<table width="100%" border="0">
		<tr>
			<td width="100%"><span name="table" id="table"></span></td>
		</tr>
	</table>
</body>
</html>


 
Hmmm...

How about, instead of this:

Code:
var outputFrame = top.searchResults;

function onLoad()
{        
 outputFrame.document.getElementById("text").style.visibility = 'hidden';
 outputFrame.document.getElementById("text").value = "";
}

...you try this:

Code:
[b]var outputFrame;[/b]

function onLoad()
{
 [b]outputFrame = top.searchResults;[/b]
 outputFrame.document.getElementById("text").style.visibility = 'hidden';
 outputFrame.document.getElementById("text").value = "";
}

The problem might be that when you have outputFrame being set initially, there is nothing IN that frame, so it's set to nothing.

Also, you probably have noticed that while you're trying to grab the document's element with id='text', that you have no such element in searchResults.asp.

'hope something in that helps!

--Dave
 
Thanks Dave,

That now works - well until i get to the next stage!

I just couldn't see that, staring at it for too long

Much appreciated
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top