Javascript to access main document from within iframe question

V

VAXman-

First off, I'm new here.

I have a question about accessing an element in Javascript. Here's a
simple example:

</html></body>
<div id="div1"> ... </div>

<iframe... src="./javascript_in_here.html" ...></iframe>

<div id="div2"> ... </div>
</body></html>


In the iframe linked page is a Javascript procedure. I cannot seem to get
to the objects (such as div1 and div2) on the main document. Everything I
try seems to return a NULL or error.

Any assistance greatly appreciated.
 
S

stes

Ty this:
<html><body><!-- a.htm -->
<div id="div1">1</div>
<iframe src="./b.htm" ></iframe>
<div id="div2">2</div>
</body></html>

<html><head><!-- b.htm -->
<script type="text/javascript">
function f(){
alert(top.document.getElementById('div1').innerHTML+'
'+top.document.getElementById('div2').innerHTML);
}
</script></head><body onload="f()"></body></html>
 
S

stes

Try this:
<html><body><!-- a.htm -->
<div id="div1">1</div>
<iframe src="./b.htm" ></iframe>
<div id="div2">2</div>
</body></html>

<html><head><!-- b.htm -->
<script type="text/javascript">
function f(){
alert(top.document.getElementById('div1').innerHTML+'
'+top.document.getElementById('div2').innerHTML);
}
</script></head><body onload="f()"></body></html>
 
S

stes

Try this:
<html><body><!-- a.htm -->
<div id="div1">1</div>
<iframe src="./b.htm" ></iframe>
<div id="div2">2</div>
</body></html>

<html><head><!-- b.htm -->
<script type="text/javascript">
function f(){
alert(
top.document.getElementById('div1').innerHTML
+' '
+top.document.getElementById('div2').innerHTML);
}
 
S

stes

Try this:
<html><body><!-- a.htm -->
<div id="div1">1</div>
<iframe src="./b.htm" ></iframe>
<div id="div2">2</div>
</body></html>
<html><head><!-- b.htm -->
<script type="text/javascript">
function f(){
alert(
top.document.getElementById('div1').innerHTML
+' '
+top.document.getElementById('div2').innerHTML);
}
 
S

stes

Try this:
<html><body><!-- a.htm -->
<div id="div1">1</div>
<iframe src="./b.htm" ></iframe>
<div id="div2">2</div>
</body></html>
<html><head><!-- b.htm -->
<script type="text/javascript">
function f(){
alert(
top.document.getElementById('div1').innerHTML
+' '
+top.document.getElementById('div2').innerHTML);}
 
S

stes

<html><body><!-- a.htm -->
<div id="div1">1</div>
<iframe src="./b.htm" ></iframe>
<div id="div2">2</div>
</body></html>
<html><head><!-- b.htm -->
<script type="text/javascript">
function f(){
alert(top.document.getElementById('div1').innerHTML
+' '+top.document.getElementById('div2').innerHTML);
}
</script></head><body onload="f()"></body></html>
 

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

No members online now.

Forum statistics

Threads
474,160
Messages
2,570,889
Members
47,420
Latest member
ZitaVos505

Latest Threads

Top