1

I have a file x.xhtml and another file main.html, in which I am using <iframe src=x.xhtml id=childframe>. Now what I want to do is, after the file is loaded, I want to get the source of the child frame, i.e x.xhtml, using JavaScript.

I tried the following code

function getChildInput() {
 var iframe = document.getElementById('childFrame').contentWindow; 
 var childText = iframe.document.getElementById('childText'); 
 alert(iframe.document.getElementsByTagName('html')[0].innerHTML); 
}

but it didn't work for .xhtml. If I am using .html instead, it works fine.

Is this a problem with XHTML or is there any other way of getting the source from the child frame other than HTML?

Felix Kling
820k181 gold badges1.1k silver badges1.2k bronze badges
asked Mar 19, 2011 at 11:06
3
  • Is the extension the only difference? Commented Mar 19, 2011 at 11:16
  • yes! extension is the only difference when i am changing the same file to x.html with works fine Commented Mar 19, 2011 at 11:21
  • 1
    Have a look at the console which error message you get. Commented Mar 19, 2011 at 11:46

2 Answers 2

1

Try alert(iframe.document.body.innerHTML); or

var doc_iframe = document.getElementsByName("myFrame")[0].contentWindow.document;

HTH

Ivo Stoykov

answered Mar 19, 2011 at 11:17
Sign up to request clarification or add additional context in comments.

Comments

0

Try using the documentElement property:

alert(iframe.document.documentElement.innerHTML);
answered Mar 19, 2011 at 11:46

5 Comments

Can you give us the Content-Type header of the server response for the iframe, using a tool such as Fiddler? Assuming your iframe actually contains something, and is served from the same domain as your main page, there might be something wrong there.
@Frédéric Hamidi i am using firefox , moreover when i try to open the file in internet explorer its pops out download box seems IE dosent handle xml files
@imran, if the web server is under your control, try to serve .xhtml files as text/xml instead. That will solve IE's "download box" problem and might help in Firefox.
@^^ thanks i got the problem solved var iframe = document.getElementById('childFrame').contentDocument; //var childText = iframe.document.getElementById('childText'); alert(iframe.documentElement.textContent);
@imran, glad to hear that :) Please consider updating your question with your solution, or maybe replying with your own answer, so future readers can easily find it.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.