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
-
Is the extension the only difference?pimvdb– pimvdb2011年03月19日 11:16:59 +00:00Commented Mar 19, 2011 at 11:16
-
yes! extension is the only difference when i am changing the same file to x.html with works fineimran– imran2011年03月19日 11:21:15 +00:00Commented Mar 19, 2011 at 11:21
-
1Have a look at the console which error message you get.Felix Kling– Felix Kling2011年03月19日 11:46:11 +00:00Commented Mar 19, 2011 at 11:46
2 Answers 2
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
i100
4,6661 gold badge26 silver badges21 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
Try using the documentElement property:
alert(iframe.document.documentElement.innerHTML);
answered Mar 19, 2011 at 11:46
Frédéric Hamidi
264k42 gold badges497 silver badges486 bronze badges
5 Comments
Frédéric Hamidi
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.imran
@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
Frédéric Hamidi
@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.imran
@^^ thanks i got the problem solved var iframe = document.getElementById('childFrame').contentDocument; //var childText = iframe.document.getElementById('childText'); alert(iframe.documentElement.textContent);
Frédéric Hamidi
@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.
default