0

The question How do I get the entire page's HTML with jQuery? might look similar, which helps in obtaining data from <html> and <!DOCTYPE>. But in addition, I also require to obtain any comments that persist before the <html> tag.

I am displaying the page with the help of srcdoc attribute using jQuery. My current code to obtain the data is

$("#myiframe").contents().find('html')[0].outerHTML;

and a snippet in this answer to obtain <!DOCTYPE html>

Sample use case:

<!-- 
  This comment will be used for further processing
--> 
<!DOCTYPE html>
<html lang='en'>
<head>
...
</head>
<body>
...
</body>
</html>
<!-- 
  This comment will be used for further processing
-->

The current output is only from <!DOCTYPE html> to </html>. Expected to have the comments outside.

Community
  • 1
  • 1
Saravanan
  • 1,879
  • 2
  • 27
  • 30

1 Answers1

0

Have a look at jQuery's $.get function. This basically returns everything you would retrieve with a normal GET request. https://api.jquery.com/jquery.get/

Try this:

$('iframe#myiframe').load(function() {
  getFrameContent(this.contentWindow.location);
});

function getFrameContent(windowURL) {
   $.get( windowURL, function( data ) {
     //where 'data' is the page contents
     $( "#whateverElementYouWant" ).html( data );
  });
}
Mark
  • 11
  • 4
  • Tried and ended up with the error `XMLHttpRequest cannot load about:srcdoc. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource.`. My `$("#myiframe").get(0).contentWindow.location` gives `about:srcdoc` – Saravanan Jan 27 '16 at 08:28
  • Updated my answer with a slightly different option. Should update the `windowURL` variable whenever the page in your ` – Mark Jan 27 '16 at 15:27