first commit

This commit is contained in:
aschwarz
2023-04-25 13:25:59 +02:00
commit 086d1e1e9e
1774 changed files with 396049 additions and 0 deletions

View File

@ -0,0 +1,40 @@
## 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](../js) folder. The first ([iframeResizer.min.js](https://raw.githubusercontent.com/davidjbradshaw/iframe-resizer/master/js/iframeResizer.min.js)) is for the page hosting the iFrames. It can be called with **native** JavaScript;
```js
const iframes = iFrameResize( [{options}], [css selector] || [iframe] );
```
The second file ([iframeResizer.contentWindow.min.js](https://raw.github.com/davidjbradshaw/iframe-resizer/master/js/iframeResizer.contentWindow.min.js)) needs placing in the page(s) contained within your iFrame. <i>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</i>.
### 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.
```html
<style>
iframe {
width: 1px;
min-width: 100%;
}
</style>
<iframe id="myIframe" src="https://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](troubleshooting.md) section.
### Example
To see this working take a look at this [example](https://davidjbradshaw.com/iframe-resizer/example/) and watch the [console](https://developer.mozilla.org/en-US/docs/Tools/Web_Console).

View File

@ -0,0 +1,15 @@
## IFrame Page Events
The following events can be included in the [options](options.md) object attached to the iframed page.
### onMessage
type: function (message)
Receive message posted from the parent page with the `iframe.iFrameResizer.sendMessage()` method.
### onReady
type: function()
This function is called once iFrame-Resizer has been initialized after receiving a call from the parent page. If you need to call any of the [parentIFrame methods](https://github.com/davidjbradshaw/iframe-resizer/blob/master/docs/iframed_page/methods.md) during page load, then they should be called from this event handler.

View File

@ -0,0 +1,78 @@
## IFrame Page Methods
These methods are available in the iFrame via the `window.parentIFrame` object. These method should be contained by a test for the `window.parentIFrame` object, in case the page is not loaded inside an iFrame. For example:
```js
if ('parentIFrame' in window) {
parentIFrame.close()
}
```
### autoResize([bool])
Turn autoResizing of the iFrame on and off. Returns bool of current state.
### close()
Remove the iFrame from the parent page.
### getId()
Returns the ID of the iFrame that the page is contained in.
### getPageInfo(callback || false)
Ask the containing page for its positioning coordinates. You need to provide a callback which receives an object with the following properties:
* **iframeHeight** The height of the iframe in pixels
* **iframeWidth** The width of the iframe in pixels
* **offsetLeft** The number of pixels between the left edge of the containing page and the left edge of the iframe
* **offsetTop** The number of pixels between the top edge of the containing page and the top edge of the iframe
* **scrollLeft** The number of pixels between the left edge of the iframe and the left edge of the iframe viewport
* **scrollTop** The number of pixels between the top edge of the iframe and the top edge of the iframe viewport
* **documentHeight** The containing document's height in pixels (the equivalent of `document.documentElement.clientHeight` in the container)
* **documentWidth** The containing document's width in pixels (the equivalent of `document.documentElement.clientWidth` in the container)
* **windowHeight** The containing window's height in pixels (the equivalent of `window.innerHeight` in the container)
* **windowWidth** The containing window's width in pixels (the equivalent of `window.innerWidth` in the container)
* **clientHeight** (deprecated) The height of the containing document, considering the viewport, in pixels (`max(documentHeight, windowHeight)`).
* **clientWidth** (deprecated) The width of the containing document, considering the viewport, in pixels (`max(documentWidth, windowWidth)`).
Your callback function will be recalled when the parent page is scrolled or resized.
Pass `false` to disable the callback.
### scrollTo(x,y)
Scroll the parent page to the coordinates x and y.
### scrollToOffset(x,y)
Scroll the parent page to the coordinates x and y relative to the position of the iFrame.
### sendMessage(message,[targetOrigin])
Send data to the containing page, `message` can be any data type that can be serialized into JSON. The `targetOrigin` option is used to restrict where the message is sent to; to stop an attacker mimicking your parent page. See the MDN documentation on [postMessage](https://developer.mozilla.org/en-US/docs/Web/API/Window.postMessage) for more details.
### setHeightCalculationMethod(heightCalculationMethod)
Change the method use to workout the height of the iFrame.
### size ([customHeight],[ customWidth])
Manually force iFrame to resize. This method optionally accepts two arguments: **customHeight** & **customWidth**. To use them you need first to disable the `autoResize` option to prevent auto resizing and enable the `sizeWidth` option if you wish to set the width.
```js
iFrameResize({
autoResize: false,
sizeWidth: true
})
```
Then you can call the `size` method with dimensions:
```js
if ('parentIFrame' in window) {
parentIFrame.size(100); // Set height to 100px
}
```

View File

@ -0,0 +1,28 @@
## IFrame Page Options
The following options can be set from within the iFrame page by creating a `window.iFrameResizer` object before the JavaScript file is loaded into the page.
```html
<script>
window.iFrameResizer = {
targetOrigin: 'https://mydomain.com'
}
</script>
<script src="js/iframeresizer.contentwindow.js"></script>
```
### targetOrigin
default: '*'
type: string
This option allows you to restrict the domain of the parent page, to prevent other sites mimicking your parent page.
### heightCalculationMethod / widthCalculationMethod
default: null
type: string | function() { return integer }
These options can be used to override the option set in the parent page (See above for details on available values). This can be useful when moving between pages in the iFrame that require different values for these options.
Altenatively you can pass a custom function that returns the size as an integer. This can be useful when none of the standard ways of working out the size are suitable. However, normally problems with sizing are due to CSS issues and this should be looked at first.

View File

@ -0,0 +1,67 @@
## Events
The following callback events can be passed to iframe-resizer on the parent page, as part of the [options](options.md) object.
### onClose
```js
onClose: (iframeID) => boolean
```
Called before iFrame is closed via `parentIFrame.close()` or `iframe.iFrameResizer.close()` methods. Returning `false` will prevent the iFrame from closing.
### onClosed
```js
onClosed: (iframeID) => undefined
```
Called after iFrame is closed via `parentIFrame.close()` or `iframe.iFrameResizer.close()` methods.
### onInit
```js
onInit: (iframe) => undefined
```
Called after initial setup.
### onMessage
```js
onMessage: ({iframe,message}) => undefined
```
Receive message posted from iFrame with the `parentIFrame.sendMessage()` method.
### onMouseEnter
```js
onMouseEnter: ({iframe,height,width,type}) => undefined
```
Function called after the mouse enters the iframe. Passes `messageData` object containing the **iFrame**, **screenX**, **screenY** and the **type** of event that triggered the callback.
### onMouseLeave
```js
onMouseLeave: ({iframe,height,width,type}) => undefined
```
Function called after the mouse leaves the iframe. Passes `messageData` object containing the **iFrame**, **screenX**, **screenY** and the **type** of event that triggered the callback.
### onResized
```js
onResized: ({iframe,height,width,type}) => undefined
```
Function called after iFrame resized. Passes `messageData` object containing the **iFrame**, **height**, **width** and the **type** of event that triggered the iFrame to resize.
### onScroll
```js
onScroll: ({x,y}) => [true|false]
```
Called before the page is repositioned after a request from the iFrame, due to either an in page link, or a direct request from either [parentIFrame.scrollTo()](../iframed_page/methods.md#scrolltoxy) or [parentIFrame.scrollToOffset()](../iframed_page/methods.md#scrolltooffsetxy). If this function returns **false**, it will stop the library from repositioning the page, so that you can implement your own animated page scrolling instead.

View File

@ -0,0 +1,23 @@
## IFrame Object Methods
Once the iFrame has been initialized, an `iFrameResizer` object is bound to it. This has the following methods available.
### close()
Remove the iFrame from the page.
### moveToAnchor(anchor)
Move to anchor in iFrame.
### removeListeners()
Detach event listeners from iFrame. This is option allows Virtual DOMs to remove an iFrame tag. It should not normally be required.
### resize()
Tell the iFrame to resize itself.
### sendMessage(message, [targetOrigin])
Send data to the containing page, `message` can be any data type that can be serialized into JSON. The `targetOrigin` option is used to restrict where the message is sent to, in case your iFrame navigates away to another domain.

View File

@ -0,0 +1,162 @@
## Options
The following options can be passed to iframe-resizer on the parent page.
### log
default: false
type: boolean
Setting the `log` option to true will make the scripts in both the host page and the iFrame output everything they do to the JavaScript console so you can see the communication between the two scripts.
### autoResize
default: true
type: boolean
When enabled changes to the Window size or the DOM will cause the iFrame to resize to the new content size. Disable if using size method with custom dimensions.
<i>Note: When set to false the iFrame will still inititally size to the contained content, only additional resizing events are disabled.</i>
### bodyBackground
default: null
type: string
Override the body background style in the iFrame.
### bodyMargin
default: null
type: string || number
Override the default body margin style in the iFrame. A string can be any valid value for the CSS margin attribute, for example '8px 3em'. A number value is converted into px.
### bodyPadding
default: null
type: string || number
Override the default body padding style in the iFrame. A string can be any valid value for the CSS margin attribute, for example '8px 3em'. A number value is converted into px.
### checkOrigin
default: true
type: boolean || array
When set to true, only allow incoming messages from the domain listed in the `src` property of the iFrame tag. If your iFrame navigates between different domains, ports or protocols; then you will need to provide an array of URLs or disable this option.
### inPageLinks
default: false
type: boolean
When enabled in page linking inside the iFrame and from the iFrame to the parent page will be enabled.
### heightCalculationMethod
default: 'bodyOffset'
values: 'bodyOffset' | 'bodyScroll' | 'documentElementOffset' | 'documentElementScroll' |
'max' | 'min' | 'grow' | 'lowestElement' | 'taggedElement'
By default the height of the iFrame is calculated by converting the margin of the `body` to <i>px</i> and then adding the top and bottom figures to the offsetHeight of the `body` tag.
In cases where CSS styles causes the content to flow outside the `body` you may need to change this setting to one of the following options. Each can give different values depending on how CSS is used in the page and each has varying side-effects. You will need to experiment to see which is best for any particular circumstance.
* **bodyOffset** uses `document.body.offsetHeight`
* **bodyScroll** uses `document.body.scrollHeight` <sup>*</sup>
* **documentElementOffset** uses `document.documentElement.offsetHeight`
* **documentElementScroll** uses `document.documentElement.scrollHeight` <sup>*</sup>
* **max** takes the largest value of the main four options <sup>*</sup>
* **min** takes the smallest value of the main four options <sup>*</sup>
* **lowestElement** Loops though every element in the DOM and finds the lowest bottom point <sup></sup>
* **taggedElement** Finds the bottom of the lowest element with a `data-iframe-height` attribute
<i>Notes:</i>
<i>**If the default option doesn't work then the best solutions is to use either** taggedElement, **or** lowestElement</i>**.** Alternatively it is possible to add your own custom sizing method directly inside the iFrame, see the [iFrame Page Options](../iframed_page/options.md) section for more details.
<sup></sup> <i>The **lowestElement** option is the most reliable way of determining the page height. However, it does have a performance impact, as it requires checking the position of every element on the page. The **taggedElement** option provides much greater performance by limiting the number of elements that need their position checked</i>.
<sup>*</sup> These methods can cause screen flicker in some browsers.
### maxHeight / maxWidth
default: infinity
type: integer
Set maximum height/width of iFrame.
### minHeight / minWidth
default: 0
type: integer
Set minimum height/width of iFrame.
### resizeFrom
default: 'parent'
values: 'parent', 'child'
Listen for resize events from the parent page, or the iFrame. Select the 'child' value if the iFrame can be resized independently of the browser window. <i>Selecting this value can cause issues with some height calculation methods on mobile devices</i>.
### scrolling
default: false
type: boolean | 'omit'
Enable scroll bars in iFrame.
* **true** applies `scrolling="yes"`
* **false** applies `scrolling="no"`
* **'omit'** applies no `scrolling` attribute to the iFrame
### sizeHeight
default: true
type: boolean
Resize iFrame to content height.
### sizeWidth
default: false
type: boolean
Resize iFrame to content width.
### tolerance
default: 0
type: integer
Set the number of pixels the iFrame content size has to change by, before triggering a resize of the iFrame.
### widthCalculationMethod
default: 'scroll'
values: 'bodyOffset' | 'bodyScroll' | 'documentElementOffset' | 'documentElementScroll' |
'max' | 'min' | 'scroll' | 'rightMostElement' | 'taggedElement'
By default the width of the page is worked out by taking the greater of the **documentElement** and **body** scrollWidth values.
Some CSS techniques may require you to change this setting to one of the following options. Each can give different values depending on how CSS is used in the page and each has varying side-effects. You will need to experiment to see which is best for any particular circumstance.
* **bodyOffset** uses `document.body.offsetWidth`
* **bodyScroll** uses `document.body.scrollWidth` <sup>*</sup>
* **documentElementOffset** uses `document.documentElement.offsetWidth`
* **documentElementScroll** uses `document.documentElement.scrollWidth` <sup>*</sup>
* **scroll** takes the largest value of the two scroll options <sup>*</sup>
* **max** takes the largest value of the main four options <sup>*</sup>
* **min** takes the smallest value of the main four options <sup>*</sup>
* **rightMostElement** Loops though every element in the DOM and finds the right most point <sup></sup>
* **taggedElement** Finds the left most element with a `data-iframe-width` attribute
Alternatively it is possible to add your own custom sizing method directly inside the iFrame, see the [iFrame Page Options](../iframed_page/options.md) section for more details
<sup></sup> <i>The **rightMostElement** option is the most reliable way of determining the page width. However, it does have a performance impact as it requires calculating the position of every element on the page. The **taggedElement** option provides much greater performance by limiting the number of elements that need their position checked.</i>
<sup>*</sup> These methods can cause screen flicker in some browsers.

View File

@ -0,0 +1,19 @@
# iFrame-Resizer Documentation
- [Getting Started](getting_started.md)
- **Parent Page API**
- [Options](parent_page/options.md)
- [Events](parent_page/events.md)
- [Methods](parent_page/methods.md)
- **IFramed Page API**
- [Options](iframed_page/options.md)
- [Events](iframed_page/events.md)
- [Methods](iframed_page/methods.md)
- **Use with Libraries and Frameworks**
- [React](https://github.com/davidjbradshaw/iframe-resizer-react)
- [Vue](https://github.com/davidjbradshaw/iframe-resizer/blob/master/docs/use_with/vue.md)
- [Angular](https://github.com/davidjbradshaw/iframe-resizer/issues/478#issuecomment-347958630)
- [jQuery](use_with/jquery.md)
- [Troubleshooting](troubleshooting.md)
- [Upgrade from version 3](upgrade.md)
- [Version history](../CHANGELOG.md)

View File

@ -0,0 +1,119 @@
## Troubleshooting
The first steps to investigate a problem is to make sure you are using the latest version and then enable the [log](#log) option, which outputs everything that happens to the [JavaScript Console](https://developers.google.com/chrome-developer-tools/docs/console#opening_the_console). This will enable you to see what both the iFrame and host page are up to and also see any JavaScript error messages.
Solutions for the most common problems are outlined in this section. If you need further help, then please ask questions on [StackOverflow](https://stackoverflow.com/questions/tagged/iframe-resizer) with the `iframe-resizer` tag.
Bug reports and pull requests are welcome on the [issue tracker](https://github.com/davidjbradshaw/iframe-resizer/issues). Please read the [contributing guidelines](https://github.com/davidjbradshaw/iframe-resizer/blob/master/CONTRIBUTING.md) before opening a ticket, as this will ensure a faster resolution.
### Multiple IFrames on one page
When the resizer does not work using multiple IFrames on one page, make sure that each frame has an unique id or no ids at all.
### IFrame not sizing correctly
If a larger element of content is removed from the normal document flow, through the use of absolute positioning, it can prevent the browser working out the correct size of the page. In such cases you can change the [heightCalculationMethod](./parent_page/options.md#heightcalculationmethod) to uses one of the other sizing methods.
### IFrame not downsizing
The most likely cause of this problem is having set the height of an element to be 100% of the page somewhere in your CSS. This is normally on the `html` or `body` elements, but it could be on any element in the page. This can sometimes be got around by using the `taggedElement` height calculation method and added a `data-iframe-height` attribute to the element that you want to define the bottom position of the page. You may find it useful to use `position: relative` on this element to define a bottom margin or allow space for a floating footer.
Not having a valid [HTML document type](https://en.wikipedia.org/wiki/Document_type_declaration) in the iFrame can also sometimes prevent downsizing. At it's most simplest this can be the following.
```html
<!DOCTYPE html>
```
### IFrame not resizing
The most common cause of this is not placing the [iframeResizer.contentWindow.min.js](https://raw.github.com/davidjbradshaw/iframe-resizer/master/js/iframeResizer.contentWindow.min.js) script inside the iFramed page. If the other page is on a domain outside your control and you can not add JavaScript to that page, then now is the time to give up all hope of ever getting the iFrame to size to the content. As it is impossible to work out the size of the contained page, without using JavaScript on both the parent and child pages.
### IFrame not detecting CSS :hover events
If your page resizes via CSS `:hover` events, these won't be detected by default. It is however possible to create `mouseover` and `mouseout` event listeners on the elements that are resized via CSS and have these events call the [parentIFrame.size()](##parentiframesize-customheight-customwidth) method. With jQuery this can be done as follows
```js
function resize(){
if ('parentIFrame' in window) {
// Fix race condition in FireFox with setTimeout
setTimeout(parentIFrame.size.bind(parentIFrame),0);
}
}
$(*Element with hover style*).hover(resize);
```
### IFrame not detecting textarea resizes
Both FireFox and the WebKit based browsers allow the user to resize `textarea` input boxes. Unfortunately the WebKit browsers don't trigger the mutation event when this happens. This can be worked around to some extent with the following code.
```js
function store() {
this.x = this.offsetWidth
this.y = this.offsetHeight
}
$('textarea')
.each(store)
.on('mouseover mouseout', function() {
if (this.offsetWidth !== this.x || this.offsetHeight !== this.y) {
store.call(this)
if ('parentIFrame' in window) {
parentIFrame.size()
}
}
})
```
### IFrame flickers
Some of the alternate [height calculation methods](./parent_page/options.md#heightcalculationmethod), such as **max** can cause the iFrame to flicker. This is due to the fact that to check for downsizing, the iFrame first has to be downsized before the new height can be worked out. This effect can be reduced by setting a [minSize](./docs/parent_page/options.md#minheight--minwidth) value, so that the iFrame is not reset to zero height before regrowing.
In modern browsers, if the default [height calculation method](./parent_page/options.md#heightcalculationmethod) does not work, then it is normally best to use **taggedElement** or **lowestElement**, which are both flicker free.
<i>Please see the notes section under [heightCalculationMethod](./parent_page/options.md#heightcalculationmethod) to understand the limitations of the different options.</i>
### Localhost 127.0.0.1 and file:///
When an iframe is located on your local machine the browser adds extra security restrictions to cross-domain iframes. These will stop iframe-resizer from functioning. If you need to test something locally, then it is best to use the external IP Address of the machine.
### Failed to execute 'postMessage' on 'DOMWindow'
This error occurs when the parent window tries to send a message to the iframe before it has loaded. IFrameResize makes multiple attempts to talk to the iFrame, so if everything is working then you can safely ignore this error message.
If you're still having problems, or you really want to not ignore the error, then you can try delaying the call to `iframeResize()` until after the `onLoad` event of the iframe has fired.
If this does not fix the problem then check `x-Frame-Options` http header on the server that is sending the iframe content, as this can also block calls to `postMessage` if set incorrectly.
### ParentIFrame not found errors
The `parentIFrame` object is created once the iFrame has been initially resized. If you wish to use it during page load you will need call it from the onReady.
```html
<script>
window.iFrameResizer = {
onReady: function() {
var myId = window.parentIFrame.getId()
console.log('The ID of the iFrame in the parent page is: ' + myId)
}
}
</script>
<script src="js/iframeresizer.contentwindow.js"></script>
```
### PDF and OpenDocument files
It is not possible to add the required JavaScript to PDF and ODF files. However, you can get around this limitation by using [ViewerJS](https://viewerjs.org/) to render these files inside a HTML page, that also contains the iFrame JavaScript file ([iframeResizer.contentWindow.min.js](https://raw.github.com/davidjbradshaw/iframe-resizer/master/js/iframeResizer.contentWindow.min.js)).
### Unexpected message received error
By default the origin of incoming messages is checked against the `src` attribute of the iFrame. If they don't match an error is thrown. This behaviour can be disabled by setting the [checkOrigin](./docs/parent_page/options.md#checkorigin) option to **false**.
### Width not resizing
By default only changes in height are detected, if you want to calculate the width you need to set the `sizeWidth` option to true and the `sizeHeight` option to false.
### Frame has not responded within 5 seconds
This can happen when postMessage is being blocked in browser. There could be multiple reasons to that but in some cases we found that RocketLoader extension within Cloudflare was the reason. Try disabling it if you are using cloudflare.

View File

@ -0,0 +1,5 @@
## Upgrading to version 4
In version 4 support for IE 8-10 and Andriod 4.4 has been removed, if you still need this then please use [version 3](https://github.com/davidjbradshaw/iframe-resizer/tree/V3) of this library.
The callback methods have been renamed to onEvents, so for example `scrollCallback` is now called `onScroll`. This is to enable better integration with modern libraries such as React.

View File

@ -0,0 +1,7 @@
## jQuery
If jQuery is detected on the page, then this library provides a simple jQuery interface.
```js
$('iframe').iFrameResize([{ options }])
```

View File

@ -0,0 +1,30 @@
## Vue
Create the following Vue directive
```js
import Vue from 'vue'
import iframeResize from 'iframe-resizer/js/iframeResizer';
Vue.directive('resize', {
bind: function(el, { value = {} }) {
el.addEventListener('load', () => iframeResize(value, el))
},
unbind: function (el) {
el.iFrameResizer.removeListeners();
}
})
```
and then include it on your page as follows.
```html
<iframe
v-resize="{ log: true }"
width="100%"
src="myiframe.html"
frameborder="0"
></iframe>
```
- Thanks to [Aldebaran Desombergh](https://github.com/davidjbradshaw/iframe-resizer/issues/513#issuecomment-538333854) for this example