Javascript DOM HTML IFrame srcdoc Property get

Introduction

Return the HTML content that is shown in an iframe:

var x = document.getElementById("myFrame").srcdoc;

Click button to display the HTML content that is shown in the iframe.

View in separate window

<!DOCTYPE html>
<html>
<body>

<iframe id="myFrame" srcdoc="<p>Hello world!</p>" src="https://www.java2s.com"></iframe>
<button onclick="myFunction()">Test</button>

<p id="demo"></p>

<script>
function myFunction() {/*from  w w w . j  a  v  a 2 s.  c o  m*/
  var x = document.getElementById("myFrame").srcdoc;
  document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>



PreviousNext

Related