Resize iframe height according to content height in it Resize iframe height according to content height in it ajax ajax

Resize iframe height according to content height in it


To directly answer your two subquestions: No, you cannot do this with Ajax, nor can you calculate it with PHP.

What I have done in the past is use a trigger from the iframe'd page in window.onload (NOT domready, as it can take a while for images to load) to pass the page's body height to the parent.

<body onload='parent.resizeIframe(document.body.scrollHeight)'>

Then the parent.resizeIframe looks like this:

function resizeIframe(newHeight){    document.getElementById('blogIframe').style.height = parseInt(newHeight,10) + 10 + 'px';}

Et voila, you have a robust resizer that triggers once the page is fully rendered with no nasty contentdocument vs contentWindow fiddling :)

Sure, now people will see your iframe at default height first, but this can be easily handled by hiding your iframe at first and just showing a 'loading' image. Then, when the resizeIframe function kicks in, put two extra lines in there that will hide the loading image, and show the iframe for that faux Ajax look.

Of course, this only works from the same domain, so you may want to have a proxy PHP script to embed this stuff, and once you go there, you might as well just embed your blog's RSS feed directly into your site with PHP.


You can do this with JavaScript.

document.getElementById('foo').height = document.getElementById('foo').contentWindow.document.body.scrollHeight + "px";


Fitting IFRAME contents is kind of an easy thing to find on Google. Here's one solution:

<script type="text/javascript">    function autoIframe(frameId) {       try {          frame = document.getElementById(frameId);          innerDoc = (frame.contentDocument) ? frame.contentDocument : frame.contentWindow.document;          objToResize = (frame.style) ? frame.style : frame;          objToResize.height = innerDoc.body.scrollHeight + 10;       }       catch(err) {          window.status = err.message;       }    }</script>

This of course doesn't solve the cross-domain problem you are having... Setting document.domain might help if these sites are in the same place. I don't think there is a solution if you are iframe-ing random sites.