imran
imran

Reputation: 11

Getting source of iFrame

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?

Upvotes: 1

Views: 636

Answers (2)

i100
i100

Reputation: 4666

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

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

HTH

Ivo Stoykov

Upvotes: 1

Fr&#233;d&#233;ric Hamidi
Fr&#233;d&#233;ric Hamidi

Reputation: 263157

Try using the documentElement property:

alert(iframe.document.documentElement.innerHTML);

Upvotes: 0

Related Questions