Forum Moderators: open

Message Too Old, No Replies

iframe variable access from parent javascript

         

orion_rus

10:33 pm on Mar 1, 2005 (gmt 0)

10+ Year Member



Hello world i have following:

<iframe>
<script>
variable='hi';
</script>
</iframe>

I need to know how i can access to a variable with javascript from the top window?
Thanks for helping me..

Rambo Tribble

3:08 am on Mar 2, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you give the iframe a name, you can use it to address the variable with dot notation. Consider the following:

Document 1:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Untitled</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
</head>
<body>
<iframe name="ifrOne" src="[i]your_file.htm[/i]"></iframe>
<p>
<br />
<a href="#" onclick="alert(window.ifrOne.test_var);return false;">check variable</a>
</p>
</body>
</html>

Document 2 (the one the iframe src points to):


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Untitled</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
var test_var="testing, testing . . .";
</script>
</head>
<body>
<div style="height:200px;width:300px;background:#dee;"></div>
</body>
</html>

orion_rus

10:51 am on Mar 2, 2005 (gmt 0)

10+ Year Member



Thank you very much) it works great now!