Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic height of iframe

I am currently working on an existing site that uses iframes(sigh!), these are pretty painful to work with and I'm having problems getting the height of the iframe to scale with the height of the HTML content.

I've tried a few different script snippets that floats around on the interwebs, but none of them have worked for me. I really want to use jQuery to do this, but that's not an option because IT wants to keep the size of the pageload down.

Does anyone know of good way to do this, in a way that works on both FF and IE 6+?

like image 907
timkl Avatar asked Mar 30 '09 14:03

timkl


People also ask

How can I set dynamic height in iframe?

Answer: Use the contentWindow Property You can use the JavaScript contentWindow property to make an iFrame automatically adjust its height according to the contents inside it, so that no vertical scrollbar will appear.

How do I make my iframe height 100%?

To size the <iframe> , we ignore the width="560" height="315" element properties, and instead use the CSS properties width:100%;height:100%; . That's it: a full-width <iframe> with fixed aspect ratio.

What is iframe height?

The height attribute specifies the height of an <iframe> , in pixels. The default height is 150 pixels.


1 Answers

You should just be able to simply set the height and the width parameters - since these are both valid attributes of the iframe dom element.

function resize(width, height) {
  var frame = document.getElementById('my_iframe');
  frame.width = width;
  frame.height = height;
}

Of course, this only applies if you are attempting to resize the iframe from it's parent element (the document with the actual iframe tag). If you are trying to resize the iframe from within the iframe (the document the iframe loads) you will need to call a public function of the parent element to perform the resize.

In iframe: parent.resize(600, 800);

like image 163
Michael Wales Avatar answered Oct 23 '22 03:10

Michael Wales