access iframe content?

C

carlos

I am trying to access the content of my iframe src page. Something
like this:

document.getElementById('myFrame').contentWindow.document;

It works fine on a predifined iframe element, However, when creating
the iframe element
dynamically, and appending it to the body, I can't access the content
when doing this dynamic insertion. My guess would be that this is
because the iframe element doesn't yet exist on the Dom. See example
of problem below:

var myFrame = document.createElement("iframe");
myFrame.id = "testID";
myFrame.name = "testID";
myFrame.src = "testPage.htm";
myFrame.frameBorder = "0";

document.body.appendChild(myFrame);

//code below isn't working due to the above
document.getElementById('testID').contentWindow.document;

Does anyone have an example of what I can do to resolve this issue?

Thanks
 
S

Stevo

carlos said:
document.getElementById('myFrame').contentWindow.document;

It works fine on a predifined iframe element, However, when creating
the iframe element
dynamically, and appending it to the body, I can't access the content
when doing this dynamic insertion. My guess would be that this is
because the iframe element doesn't yet exist on the Dom.

I don't think it's because the element doesn't exist in the DOM. It's
more likely that the document doesn't exist yet inside the iframe.

You're better off exposing a function in the top page that the iframe
itself can script up to. It can basically call you and say I'm here and
ready. At which point you can then script to it.
 
C

carlos

I don't think it's because the element doesn't exist in the DOM. It's
more likely that the document doesn't exist yet inside the iframe.

You're better off exposing a function in the top page that the iframe
itself can script up to. It can basically call you and say I'm here and
ready. At which point you can then script to it.

Thanks for the response! That should do the trick..
 

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
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top