How do I adjust the height of an iframe according to the height of its content? I have read other answers in stackoverflow. But those answers were not working for me. I have 3 iframes in index.html. I want to adjust the height of the second iframe whose id is myIframe. This is my index.html (The code works in internet explorer but not in chrome and firefox):
<!DOCTYPE html>
<html>
<body>
<div style="display: flex; flex-direction: column; align-items: stretch;">
<iframe src="header.html" scrolling="no" style="width: 100%;border: 0;
height:50px"></iframe>
<iframe class="" id="myIframe" onload = "changeheight()"
name="a" scrolling="no" style="border:none; width: 100%; min-
height:calc(100vh - 115px);" allowtransparency="true" allowfullscreen>
</iframe>
<iframe src="footer.html" scrolling="no" style="width: 100%;border: 0;
height:50px;"></iframe>
</div>
<script type='text/javascript'>
function changeheight() {
document.getElementById('myIframe').style.height =
document.getElementById('myIframe').contentWindow.document.body.scrollHeight
+ 'px';
}
</script>
</body>
</html>
and my header.html is
<!DOCTYPE html>
<html>
<body>
<header style="padding: 20px; background: pink">
<a target="a" href="about.html">About us</a>
<a target="a" href="contact.html">Contact us</a>
</header>
</body>
</html>
about.html and contact.html contain some dummy data.
I am getting these two errors in chrome in the console -
index.html:22 Uncaught ReferenceError: changeheight is not defined
at HTMLIFrameElement.onload (index.html:22)
index.html:82 Uncaught DOMException: Blocked a frame with origin "null" from
accessing a cross-origin frame.
I don't understand why I am getting the first error. I have defined changeheight in javascript.
I don't understand the second error at all.