A modern, lightweight alternative for resizing iframes dynamically. It is shipped under the MIT license, making it usable in commercial projects.
If you found this project helpful, please consider starring the repository!
<script type="module">
import { initialize } from "https://cdn.jsdelivr.net/npm/@open-iframe-resizer/core@latest/dist/index.min.js";
initialize({}, "#my-iframe");
</script>
You can found a working example here
Note you can also install the core package through npm:
npm install @open-iframe-resizer/core
A React component is also available:
npm install @open-iframe-resizer/react
You can execute a custom function after an iframe has been resized. Also, you can use built-in functions
like updateParentScrollOnResize
to help keep the iframe within the viewport after resizing:
import { initialize, updateParentScrollOnResize } from "https://cdn.jsdelivr.net/npm/@open-iframe-resizer/core@latest/dist/index.min.js";
initialize({ onIframeResize: updateParentScrollOnResize }, "#myIframe");
-
If you have control over the embedded page, you need to load the script on your child page to enable messaging between the two windows (you do not need to call the initialize function in the child; loading the module is sufficient).
Here is an example of the parent page and the child.
-
If you have no control over the child iframe domain, and, by chance, the child page loads the legacy iframe-resizer script, you can initialize the library with the compatibility mode; it will try to connect to the child iframe:
initialize({ enableLegacyLibSupport: true }, "#my-iframe");
This library is very good, but it has changed its license, so it is no longer usable in closed-source projects for free. I decided to replicate some parts of the API, as it may facilitate migration to this project.
Some features from this library are missing, but they could be implemented in future versions.
Chrome | Safari | Firefox | Opera | IE |
---|---|---|---|---|
64+ | 13.1+ | 69+ | 51+ | 🙅♂️ |