A lightweight JavaScript library for seamless iframe communication and automatic resizing.
- Automatic iframe height and width adjustment
- Two-way communication between parent and child frames
- Scroll position synchronization
- Custom event handling
- Ready-state detection
- Configurable logging
- Singleton pattern in a child frame
Add both scripts to your project:
<!-- In parent page -->
<script src="/dist/iframe-resizer-parent.min.js"></script>
<!-- In child page -->
<script src="/dist/iframe-resizer-child.min.js"></script>Basic setup:
const resizer = new IFrameResizer('#myIframe', {
log: true,
onResize: (width, height) => {
console.log(`Iframe resized to ${width}x${height}`);
}
}).onReady(() => {
console.log('Iframe is ready!');
});Advanced usage with custom messages:
const resizer = new IFrameResizer('#myIframe', {
targetOrigin: 'https://child-domain.com',
log: true
});
// Handle custom messages
resizer.onMessage('customEvent', (payload) => {
console.log('Custom event received:', payload);
});
// Send message to child
resizer.sendMessage('updateContent', {data: 'Hello Child!'});Basic setup:
window.IFrameResizer.create({
log: true
});Advanced usage with custom messages:
const resizer = window.IFrameResizer.create({
targetOrigin: 'https://parent-domain.com',
log: true
});
// Handle custom messages
resizer.onMessage('updateContent', (payload) => {
console.log('Update received:', payload);
});
// Send message to parent
resizer.sendMessage('customEvent', {data: 'Hello Parent!'});| Option | Type | Default | Description |
|---|---|---|---|
| targetOrigin | string | '*' | Allowed origin for postMessage |
| log | boolean | false | Enable console logging |
| onResize | function | null | Callback for resize events |
| onScroll | function | null | Callback for scroll events |
| Option | Type | Default | Description |
|---|---|---|---|
| targetOrigin | string | '*' | Allowed origin for postMessage |
| log | boolean | false | Enable console logging |
| resize | boolean | true | Enable auto-resizing |
| scroll | boolean | true | Enable scroll tracking |
onReady(callback): Register callback for iframe ready stateonMessage(type, callback): Register custom message handlersendMessage(type, data): Send a message to child iframedestroy(): Clean up event listeners
onMessage(type, callback): Register custom message handlersendMessage(type, data): Send a message to parentdestroy(): Clean up instance and listeners
ready: Sent when child iframe is initializedresize: Triggered on size changesscroll: Triggered on scroll position changes
You can define and handle custom events using onMessage() and sendMessage().
- All modern browsers (Chrome, Firefox, Safari, Edge)
- Requires
ResizeObserversupport (or polyfill) - Requires
postMessagesupport
MIT License
Feel free to submit issues and pull requests.