What is iframe-resizer?
The iframe-resizer npm package provides a set of tools to automatically resize iframes to fit their content and to enable communication between the iframe and the parent page. It is useful for ensuring that iframes are displayed correctly without scrollbars and for passing data between the iframe and the parent page.
What are iframe-resizer's main functionalities?
Automatic Resizing
This feature automatically resizes the iframe to fit its content. The `log: true` option enables logging for debugging purposes, and `#myIframe` is the selector for the iframe element.
const iFrameResize = require('iframe-resizer/js/iframeResizer');
iFrameResize({ log: true }, '#myIframe');
Height Calculation Methods
This feature allows you to specify different methods for calculating the height of the iframe. The `heightCalculationMethod` option can be set to various values like 'bodyScroll', 'documentElementScroll', 'max', etc. In this example, 'taggedElement' is used.
iFrameResize({ heightCalculationMethod: 'taggedElement' }, '#myIframe');
Message Passing
This feature enables sending messages from the parent page to the iframe. The `sendMessage` method is used to send a message to the iframe.
const iframe = iFrameResize({ log: true }, '#myIframe')[0];
iframe.iFrameResizer.sendMessage('Hello from parent');
Event Handling
This feature allows you to handle events such as resizing. The `resizedCallback` option is a function that gets called whenever the iframe is resized.
iFrameResize({
resizedCallback: function(messageData) {
console.log('Iframe resized:', messageData);
}
}, '#myIframe');
Other packages similar to iframe-resizer
react-iframe-resizer-super
This package is a React wrapper for iframe-resizer, providing similar functionality but specifically designed for use with React applications. It simplifies the integration of iframe-resizer in React projects.
iframe-resizer-react
Another React-specific package that wraps iframe-resizer, making it easier to use within React components. It offers similar features but is tailored for React's component-based architecture.
iFrame Resizer V4
![Donate](https://img.shields.io/badge/Donate-PayPal-blue.svg)
This library enables the automatic resizing of the height and width of both same and cross domain iFrames to fit their contained content. It provides a range of features to address the most common issues with using iFrames, these include:
- Height and width resizing of the iFrame to content size.
- Works with multiple and nested iFrames.
- Domain authentication for cross domain iFrames.
- Provides a range of page size calculation methods to support complex CSS layouts.
- Detects changes to the DOM that can cause the page to resize using MutationObserver.
- Detects events that can cause the page to resize (Window Resize, CSS Animation and Transition, Orientation Change and Mouse events).
- Simplified messaging between iFrame and host page via postMessage.
- Fixes in page links in iFrame and supports links between the iFrame and parent page.
- Provides custom sizing and scrolling methods.
- Exposes parent position and viewport size to the iFrame.
- Provides
onMouseEnter
and onMouseLeave
events for the iFrame. - Works with ViewerJS to support PDF and ODF documents.
- Supports IE 11 (V3 supports back to IE8)
Donate
Iframe-resizer is the result of many 100s of hours of work, if you would like to join others in showing support for the continued development of this project, then please feel free to buy me a coffee.
![Buy Me A Coffee](https://www.buymeacoffee.com/assets/img/custom_images/yellow_img.png)
Getting Started
Install
This package can be installed via NPM (npm install iframe-resizer --save
).
Usage
The package contains two minified JavaScript files in the js folder. The first (iframeResizer.min.js) is for the page hosting the iFrames. It can be called with via JavaScript:
const iframes = iFrameResize( [{options}], [css selector] || [iframe] );
The second file (iframeResizer.contentWindow.min.js) needs placing in the page(s) contained within your iFrame. This file is designed to be a guest on someone else's system, so has no dependencies and won't do anything until it's activated by a message from the containing page.
Typical setup
The normal configuration is to have the iFrame resize when the browser window changes size or the content of the iFrame changes. To set this up you need to configure one of the dimensions of the iFrame to a percentage and tell the library to only update the other dimension. Normally you would set the width to 100% and have the height scale to fit the content.
<style>
iframe {
width: 1px;
min-width: 100%;
}
</style>
<script src="/js/iframeResizer.min.js"></script>
<iframe id="myIframe" src="http://anotherdomain.com/iframe.html"></iframe>
<script>
iFrameResize({ log: true }, '#myIframe')
</script>
Note: Using min-width to set the width of the iFrame, works around an issue in iOS that can prevent the iFrame from sizing correctly.
If you have problems, check the troubleshooting section.
Example
To see this working take a look at this example and watch the console.
API Documentation
IFrame-Resizer provides an extensive range of options and APIs for both the parent page and the iframed page.
License
Copyright © 2013-21 David J. Bradshaw -
Licensed under the MIT License