I need a solution for auto-adjusting the width
and height
of an iframe
to barely fit its content. The point is that the width and height can be changed after the iframe
has been loaded. I guess I need an event action to deal with the change in dimensions of the body contained in the iframe.
相关问题
- Views base64 encoded blob in HTML with PHP
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- void before promise syntax
- Keeping track of variable instances
Context
I had to do this myself in a context of a web-extension. This web-extension injects some piece of UI into each page, and this UI lives inside an
iframe
. The content inside theiframe
is dynamic, so I had to readjust the width and height of theiframe
itself.I use React but the concept applies to every library.
My solution (this assumes that you control both the page and the iframe)
Inside the
iframe
I changedbody
styles to have really big dimensions. This will allow the elements inside to lay out using all the necessary space. Makingwidth
andheight
100% didn't work for me (I guess because the iframe has a defaultwidth = 300px
andheight = 150px
)Then I injected all the iframe UI inside a div and gave it some styles
After rendering my app inside this
#ui-root
(in React I do this insidecomponentDidMount
) I compute the dimensions of this div like and sync them to the parent page usingwindow.postMessage
:In the parent frame I do something like this:
I slightly modified Garnaph's great solution above. It seemed like his solution modified the iframe size based upon the size right before the event. For my situation (email submission via an iframe) I needed the iframe height to change right after submission. For example show validation errors or "thank you" message after submission.
I just eliminated the nested click() function and put it into my iframe html:
Worked for me, but not sure about cross browser functionality.
This uses inline CSS only:
All solutions given thus far only account for a once off resize. You mention you want to be able to resize the iFrame after the contents are modified. In order to do this, you need to execute a function inside the iFrame (once the contents are changed, you need to fire an event to say that the contents have changed).
I was stuck with this for a while, as code inside the iFrame seemed limited to the DOM inside the iFrame (and couldn't edit the iFrame), and code executed outside the iFrame was stuck with the DOM outside the iFrame (and couldn't pick up an event coming from inside the iFrame).
The solution came from discovering (via assistance from a colleague) that jQuery can be told what DOM to use. In this case, the DOM of the parent window.
As such, code such as this does what you need (when run inside the iFrame) :
It is possible to make a "ghost-like" IFrame that acts like it was not there.
See http://codecopy.wordpress.com/2013/02/22/ghost-iframe-crossdomain-iframe-resize/
Basically you use the event system
parent.postMessage(..)
described in https://developer.mozilla.org/en-US/docs/DOM/window.postMessageThis works an all modern browsers!
Javascript to be placed in header:
Here goes iframe html code:
Css stylesheet
>