I am loading an aspx web page in an iframe. The content in the Iframe can be of more height than the iframe's height. The iframe should not have scroll bars.
I have a wrapper div
tag inside the iframe which basically is all the content. I wrote some jQuery to make the resize happen :
$("#TB_window", window.parent.document).height($("body").height() + 50);
where
TB_window
is the div in which the Iframe
is contained.
body
- the body tag of the aspx in the iframe.
This script is attached to the iframe content. I am getting the TB_window
element from the parent page. While this works fine on Chrome, but the TB_window collapses in Firefox. I am really confused/lost on why that happens.
I found that the accepted answer didn't suffice, since X-FRAME-OPTIONS: Allow-From isn't supported in safari or chrome. Went with a different approach instead, found in a presentation given by Ben Vinegar from Disqus. The idea is to add an event listener to the parent window, and then inside the iframe, use window.postMessage to send an event to the parent telling it to do something (resize the iframe).
So in the parent document, add an event listener:
And inside the iframe, write a function to post the message:
Finally, inside the iframe, add an onLoad to the body tag to fire the resize function:
Add this to the iframe, this worked for me:
And if you use jQuery try this code:
You can refer related question here - How to make width and height of iframe same as its parent div?
To set dynamic height -
And codes - https://gist.github.com/mohandere/a2e67971858ee2c3999d62e3843889a8
I found the answer from Troy didn't work. This is the same code reworked for ajax:
This one is useful when you require a solution with no jquery. In that case you should try adding a container and set a padding to it in percentages
HTML example code:
CSS example code:
You can retrieve the height of the
IFRAME
's content by using:contentWindow.document.body.scrollHeight
After the
IFRAME
is loaded, you can then change the height by doing the following:Then, on the
IFRAME
tag, you hook up the handler like this:I had a situation a while ago where I additionally needed to call
iframeLoaded
from theIFRAME
itself after a form-submission occurred within. You can accomplish that by doing the following within theIFRAME
's content scripts: