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.
I just happened to come by your question and i have a solution. But its in jquery. Its too simple.
There you go!
Cheers! :)
Edit: If you have a Rich Text Editor based on the iframe method and not the div method and want it to expand every new line then this code will do the needful.
My workaround is to set the iframe the height/width well over any anticipated source page size in CSS & the
background
property totransparent
.In the iframe set
allow-transparency
totrue
andscrolling
tono
.The only thing visible will be whatever source file you use. It works in IE8, Firefox 3, & Safari.
I found the solution by @ShripadK most helpful, but it does not work, if there is more than one iframe. My fix is:
$('iframe').height($('iframe').contents().find('body').height() + 20)
would set the height of every frame to the same value, namely the height of the content of the first frame. So I am using jquery'sheight()
with a function instead of a value. That way the individual heights are calculated+ 20
is a hack to work around iframe scrollbar problems. The number must be bigger than the size of a scrollbar. The hack can probably be avoided but disabling the scrollbars for the iframe.setTimeout
instead ofsetInterval(..., 1)
to reduce CPU load in my caseOn 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
:And attach that to run with the iframe-body's
onLoad
event.Here is a dead simple solution that works on every browser and with cross domains:
First, this works on the concept that if the html page containing the iframe is set to a height of 100% and the iframe is styled using css to have a height of 100%, then css will automatically size everything to fit.
Here is the code:
Oli has a solution that will work for me. For the record, the page inside my iFrame is rendered by javascript, so I'll need an infinitesimal delay before reporting back the offsetHeight. It looks like something along these lines: