Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set iframe height to fit content?

I am trying to remove scrollbar from Iframe. When I set height of Iframe manually it works but Iframe size can change and I don't know how to get Iframe height when page loads.

Is there any way to remove scrollbar and fit Iframe content?

<div class="portlet" id="pageContainer">
    <iframe id="frame" width="100%" frameborder="0" height="2000" scrolling="false" allowfullscreen="" src="/app/mytestapp" style="overflow: hidden"></iframe>
</div>

When I try to below function it doesn't return correct height. (It returns 2000px but my Iframe height is nearly 3000px)

$('iframe').load(function() {        
    this.contentWindow.document.body.offsetHeight + 'px';
});
like image 832
hellzone Avatar asked Oct 18 '22 19:10

hellzone


1 Answers

You can use

$('iframe').load(function() {
    $('#frame').height($('#frame').get(0).contentWindow.document.body.offsetHeight + 'px');
})

or

$('iframe').load(function() {
    setTimeout(function(){
        $('#frame').height($('#frame').get(0).contentWindow.document.body.offsetHeight + 'px');
    }, 3000);
})
like image 138
Super User Avatar answered Oct 20 '22 10:10

Super User