0

I am using <iframe id='' src =" + url + " style='width:100%;border:none;'></iframe> I am trying to set the iframe height depending upon the content height. If I didn't mention height for the iframe,it is taking default height. can any one tell me how to set the iframe height depending upon content height....

steve
  • 664
  • 4
  • 16
  • 42

3 Answers3

0

You can refer below code:

jQuery(document).ready(function(){
var height = $(window).height();
$('iframe').css('height', height)
});

In the above example, I have set window's height to iframe. As per your requirement, you can take your content's height and then set it to iframe's height.

Hope this helps!

Vasundhara
  • 645
  • 1
  • 6
  • 9
0

HTML:

<iframe name="i_frame" id="i_frame" src="" onload="iframeAutoSize( this.id );">
</iframe>

JS:

function iframeAutoSize( id ) {

    iframe_element = document.getElementById( id );

    if ( iframe_element ){

        var new_height = iframe_element.contentDocument.documentElement.offsetHeight;
        //var new_width = iframe_element.contentDocument.documentElement.childNodes[2].offsetWidth;

        $("#"+id).height( new_height );
        //$("#"+id).width( new_width );
    }
}
steinerkelvin
  • 475
  • 2
  • 12
0

You can use below code in .

style="height:auto;"
Rahul Sahu
  • 274
  • 1
  • 4
  • 15