7

I need to get whole content of iframe from the same domain. Whole content means that I want everything starting from <html> (including), not only <body> content. Content is modified after load, so I can't get it once again from server.

user113716
  • 318,772
  • 63
  • 451
  • 440
jesper
  • 879
  • 8
  • 21

4 Answers4

18

I belive I've found the best solution:

var document = iframeObject.contentDocument;
var serializer = new XMLSerializer();
var content = serializer.serializeToString(document);

In content we have full iframe content, including DOCTYPE element, which was missing in previous solutions. And in addition this code is very short and clean.

jesper
  • 879
  • 8
  • 21
  • Thank you so much. – Rehmat Oct 10 '16 at 09:11
  • This is totally wrong, you missed a semicolon :) Thanks a lot. – Grégoire Hertault Nov 30 '16 at 17:03
  • One issue I have with this approach is that it encodes the iframe content. Everything gets Html encoded including font-family styles for example. If you have style tag with font-family defines as "Courier New", Courier, monospace then the double quotes will get encoded. – Marko Jun 05 '17 at 18:41
7

If it is on the same domain, you can just use

iframe.contentWindow.document.documentElement.innerHTML

to get the content of the iframe, except for the <html> and </html> tag, where

iframe = document.getElementById('iframeid');
Thai
  • 10,746
  • 2
  • 45
  • 57
  • I also need `` tag with all attributes. – jesper Jan 01 '11 at 19:02
  • You can loop through `iframe.contentWindow.document.documentElement`'s attribute to create the wrapping HTML tag (or use the non-standard `outerHTML`). A sample cross-browser code could be found here: http://help.dottoro.com/ljloliex.php. – Thai Jan 01 '11 at 19:08
  • 1
    The first solution is more less acceptable (can't use `outerHTML` because FF doesn't support it). It would be simplier if jQuery allowed me to wrap created `html` tag in another "wrapper" tag and the use `html()` method on that wrapper... Of course "Node cannot be inserted at the specified point in the hierarchy" – jesper Jan 01 '11 at 19:38
1
$('input.test').click(function(){
    $('textarea.test').text($('iframe.test').contents());
});
Zhasulan Berdibekov
  • 1,077
  • 3
  • 19
  • 39
0

You can get the literal source of any file on the same domain with Ajax, which does not render the html first-

//

function fetchSource(url, callback){

    try{
        var O= new XMLHttpRequest;
        O.open("GET", url, true);
        O.onreadystatechange= function(){
            if(O.readyState== 4 && O.status== 200){
                callback(O.responseText);
            }
        };
        O.send(null);
    }
    catch(er){}
    return url;
}
function printSourceCode(text){
    var el= document.createElement('textarea');
    el.cols= '80';
    el.rows= '20';
    el.value= text;
    document.body.appendChild(el);
    el.focus();
}

fetchSource(location.href, printSourceCode);

kennebec
  • 102,654
  • 32
  • 106
  • 127