Timo Limo

Navigation

Skip navigation.

Search

Site navigation

Email conversation

FromTimo Limo
ToMe
Subjectobtaining reference to element in a document inside IFRAME
Date26 May 2004 18:34
Hi Mark,
Instead of trying to invoke a webservice from javascript, I'm falling back upon 
an IFRAME approach, doing something quite similar to the example you have on 
your site. No problem setting the IFRAME's SRC or alternatively its 
window.location to the data URL (is window.location better than src= ?); however 
extracting the data from the IFRAME is causing me a problem! The 
getElementById(id) approach is not working. The document in my IFRAME contains a 
DIV (id='RESULTSPANEL') and I am having trouble getting a reference to that 
element when the time comes to extract the data from the document in the IFRAME 
and move it into my current document.
 
myDataPage.htm
<BODY>
<HEAD></HEAD>
<form name="Form1" method="post" action="FetchCenters.aspx" id="Form1">
<div id="RESULTSPANEL">
<A id=RC63>This is data line 1</A><br>
<A id=RC74>This is data line 999</a><br>
</div>
</form>
</body>
</HTML>
 
In the page that contains the IFRAME, I am having trouble referencing RESULTSPANEL:
 
function populateiframe() {
 
url='myDataPage.html';
 if( document.layers && document.layers['datadiv'].load ) {
    document.layers['datadiv'].load(url,0);
 } else if( window.frames && window.frames.length ) {
    window.frames['dataframe'].window.location.replace(url;
 } else {
   alert( 'Doesn\'t work' );
 }
 
/* now get a reference to the IFRAME */
var MYIFRAME = window.frames['dataframe'];
 
/* now try to move the contents of the iframe into current page */
var myCurrentPageTargetDiv= document.getElementById('mytargetdiv');
 
/* HERE IS WHERE THE CODE BREAKS -*/ 
var pnl=MYIFRAME.document.getElementById("RESULTSPANEL");
 
Have also tried:
 
var pnl=MYIFRAME.getElementById('RESULTSPANEL');
 
How do I reference the element in the document contained in the IFRAME?
I'm using IE6 at the moment.
Thanks!
Timo
FromMe
ToTimo Limo
SubjectRe: obtaining reference to element in a document inside IFRAME
Date26 May 2004 21:43
right, there are two 'correct' ways to do this:
the IE way;
document.getElementById('iframeID').document
and the everything else way;
document.getElementById('iframeID').contentDocument

I don't use either, because I hate code branching. Because there is the
completely cross browser way. don't ask how, don't ask why. it just works
(note, name, not ID):

document.frames['nameOfIframe'].window.document.getElementById('RESULTSPANEL')


hope this helps

Mark 'Tarquin' Wilton-Jones - author of http://www.howtocreate.co.uk/
This site was created by Mark "Tarquin" Wilton-Jones.
Don't click this link unless you want to be banned from our site.