How to Auto Height an Iframe using JavaScript

In this article I will explain how to adjust the iframe height with the content with the help of JavaScript. This is very useful when the content is dynamic and reloads or AJAX load content in an iframe.

Use below code to resize the iframe height with the content.


function resizeIframe() {
var obj = jQuery('#iframeid')[0];
var ih = obj.contentWindow.document.body.scrollHeight;
obj.style.height = ih + 'px';
}

To make it adjust with dynamic content on the fly, use interval.


setInterval(function () {
resizeIframe();
}, 1000);

Hope this helps

Share this Post