How to get WHOLE iframe content?

I need to get all iframe content from the same domain. All content means that I want everything to start with <html> (including), and not just <body> content. The content changes after loading, so I can not get it again from the server.

+6
javascript jquery iframe
source share
4 answers

I believe I have found a better solution:

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

In content we have full iframe content, including a DOCTYPE element that was not present in previous solutions. And besides, this code is very short and clean.

+15
source share

If it is in the same domain, you can simply use

 iframe.contentWindow.document.documentElement.innerHTML 

to get the contents of the iframe, with the exception of the <html> and </html> tags, where

 iframe = document.getElementById('iframeid'); 
+7
source share
 $('input.test').click(function(){ $('textarea.test').text($('iframe.test').contents()); }); 
+1
source share

You can get the literal source of any file in the same domain with Ajax, which does not display html at 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);

0
source share

All Articles