0

Assume i have iframe contain sample.html

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <link rel="import" href="sample.html">
</head>

<body>
  <p>top</p>
  <iframe src="sample.html" frameborder="0" id="myframe"></iframe>
  <p>bottom</p>
  <textarea cols="30" rows="10"></textarea>
</body>

</html>

I want to get all content include <!DOCTYPE html> ...</html>. I try to search arround the web but what i found only get content from body using

var cont = document.getElementById('myframe').contentWindow.document;
console.log(cont.body);

I don't want to use AJAX to get content file. How to get ALL content from iframe include html tag?

Aep Saepudin
  • 172
  • 2
  • 5
  • 14

3 Answers3

3

Try accessing cont.documentElement.innerHTML to get the contents of <html> tag.

If you want also the <html> tag, use cont.documentElement.outerHTML.

To get the doctype information, try using cont.doctype.

var cont = document.getElementById('myframe').contentWindow.document;
console.log(cont.documentElement.outerHTML, cont.doctype);

You could also use XMLSerializer if its supported in the browser:

console.log(new XMLSerializer().serializeToString(cont));

Martin Adámek
  • 16,771
  • 5
  • 45
  • 64
1

try this:

Using document.doctype to get the document doctype

Using document.getElementsByTagName("*"); to get most of the html

https://jsfiddle.net/4hhf6nLm/

0

following line will retrieve inner html of iframe

document.getElementById('myframe').contentWindow.document.body.innerHTML

Check this answer for more information

Dhaval Pankhaniya
  • 1,996
  • 1
  • 15
  • 26