Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to auto-size an iFrame? [duplicate]

Possible Duplicate:
Resizing an iframe based on content

I'm loading an iFrame and want the parent to automatically change the height based upon the height of the iFrame's content.

To simply things, all pages belong to the same domain, so I shouldn't run into cross-site scripting issues.

like image 959
Allan Avatar asked Oct 29 '08 14:10

Allan


People also ask

How do I adjust the iframe height automatically?

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 change the size of an iframe dynamically?

Using the window. postMessage() method, we can safely communicate between the iframe and the parent window. That way, we can send a height value from the iframe to the parent window. Then, in the parent window, we can set a simple script to dynamically update the height of the iframe.

How do I scale an iframe to fit?

What you can do is set specific width and height to your iframe (for example these could be equal to your window dimensions) and then applying a scale transformation to it. The scale value will be the ratio between your window width and the dimension you wanted to set to your iframe.

How do I make my iframe height 100%?

given the iframe is directly under body. If the iframe has a parent between itself and the body, the iframe will still get the height of its parent. One must explicitly set the height of every parent to 100% as well (if that's what one wants).


2 Answers

On any other element, I would use the scrollHeight of the DOM object and set the height accordingly. I don't know if this would work on an iframe (because they're a bit kooky about everything) but it's certainly worth a try.

Edit: Having had a look around, the popular consensus is setting the height from within the iframe using the offsetHeight:

function setHeight() {     parent.document.getElementById('the-iframe-id').style.height = document['body'].offsetHeight + 'px'; } 

And attach that to run with the iframe-body's onLoad event.

like image 110
Oli Avatar answered Oct 05 '22 13:10

Oli


Try:

  • jquery-iframe-auto-height
  • iframe-resizer
like image 35
xmedeko Avatar answered Oct 05 '22 13:10

xmedeko