Jump to content

Document-Iframe Communication


Jesdisciple
 Share

Recommended Posts

I'm trying to emulate a Java I/O interface with an iframe (for the file to be run), which requires that the iframe and its parent share values (to execute statements in the iframe and send any output back to the parent). This works fine as long as I have an alert telling me that the DOM value (of the textarea output) is recorded in a variable, but commenting the alert leaves it undefined. (I'm using PHP extensions in case the functionality comes in handy.)run.php

<html>	<head>		<title>Untitled Document</title>		<script type="text/javascript">			var env;			var inn;			var out;			function run(f){				env = document.getElementById('environment');				env.src = f;				inn = document.getElementById('input');				out = document.getElementById('output');alert(out);//Commenting this alert makes the browser forget that the variable has been defined,...				env.contentDocument.win.out = document.getElementById('output');alert(env.contentDocument.win.out);//...even if this one,...alert(out);//...or this one, remains.			}			function execute(code){alert('Executing "' + code + '"...');				env.contentDocument.win.eval(code);			}			function setup(){				alert('Setting up...');			}		</script>	</head>	<body>		<table width="100%">			<tr>				<td align="center">					<form>						<input type="file" id="file"><input type="button" value="Run" onClick="run(file.value)"><br>					</form>				</td>			</tr>			<tr>				<td align="center">					<iframe id="environment" width="100%" height="349"></iframe>				</td>			</tr>			<tr>				<td align="center">					<form>						<input type="text" id="input" size="153"><input type="button" onClick="execute(document.getElementById('input').value)" value="Input"><br>						<textarea id="output" cols="121" readonly="readonly"></textarea>					</form>				</td>			</tr>		</table>	</body></html>

You'll need to type "test.php" into the file element, push Run, type "test()" into the text field at the bottom, and push Input.test.php

<html>	<head>		<title>Untitled Document</title>		<script type="text/javascript">			document.win = window;			var out;			function println(str){alert('Printing to ' + out + '...');				out.value += str + '\n';			}			function test(){alert('Testing...');				println('Check!');			};		</script>	</head>	<body>		This page is loaded.	</body></html>

Without any alerts being commented, JSLint, Firefox, and Opera have no problems with it. IE6 fails after the first call to alert() in each thread (Run and Input), but doesn't produce an error.Is this my fault? (I don't see how it could be, but...) Is there any way to get around it?

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...