Home Gitesh Portfolio Blog About Me Gallery Contact

Get iframe height with javascript

You can retrieve the height of the IFRAME's content by using:contentWindow.document.body.scrollHeight
After the IFRAME is loaded, you can then change the height by doing the following:

<scripttype="text/javascript">
function iframeLoaded() {
var iFrameID = document.getElementById('idIframe');
if(iFrameID) {
// here you can meke the height, I delete it first, then I make it again
            iFrameID.height = "";
            iFrameID.height = iFrameID.contentWindow.document.body.scrollHeight + "px";
      }  
  }
</script>

Then, on the IFRAME tag, you hook up the handler like this:
<iframe id="idIframe" onload="iframeLoaded()" ...
I had a situation a while ago where I additionally needed to call iframeLoaded from the IFRAME itself after a form-submission occurred within. You can accomplish that by doing the following within theIFRAME's content scripts:
parent.iframeLoaded(); 

Posted: 22/04/2013 8:58:10 p.m. by Gitesh Shah | with 0 comments