@rtorcato/browser-common / iframe
iframe
Functions
isIframe()
isIframe(
el):el is HTMLIFrameElement
Defined in: iframe/index.ts:12
Checks if a given element is an iframe.
Example
import { isIframe } from '@rtorcato/browser-common/iframe'
if (isIframe(el)) console.log(el.src)
Parameters
el
unknown
The element to check.
Returns
el is HTMLIFrameElement
getIframeWindow()
getIframeWindow(
iframe):Window|null
Defined in: iframe/index.ts:26
Returns the contentWindow of an iframe, or null if not available.
Example
import { getIframeWindow } from '@rtorcato/browser-common/iframe'
const win = getIframeWindow(iframe)
Parameters
iframe
HTMLIFrameElement
The iframe element.
Returns
Window | null
getIframeDocument()
getIframeDocument(
iframe):Document|null
Defined in: iframe/index.ts:40
Returns the contentDocument of an iframe, or null if not available.
Example
import { getIframeDocument } from '@rtorcato/browser-common/iframe'
const doc = getIframeDocument(iframe)
Parameters
iframe
HTMLIFrameElement
The iframe element.
Returns
Document | null
postMessageToIframe()
postMessageToIframe(
iframe,message,targetOrigin?):void
Defined in: iframe/index.ts:60
Posts a message to the iframe's contentWindow.
Example
import { postMessageToIframe } from '@rtorcato/browser-common/iframe'
postMessageToIframe(iframe, { type: 'ping' }) // to the origin of iframe.src
postMessageToIframe(iframe, { type: 'ping' }, 'https://example.com')
Parameters
iframe
HTMLIFrameElement
The iframe element.
message
unknown
The message to send.
targetOrigin?
string
The target origin. Defaults to the origin of the iframe's src, so the
browser drops the message if the frame has navigated elsewhere. Pass '*' explicitly to send
to any origin, and never do that for sensitive data.
Returns
void
Throws
When no targetOrigin is given and the src has an opaque origin
(data:, about:blank, sandboxed), since there is no origin to default to.
setIframeSrc()
setIframeSrc(
iframe,url):void
Defined in: iframe/index.ts:86
Sets the src of an iframe safely.
Example
import { setIframeSrc } from '@rtorcato/browser-common/iframe'
setIframeSrc(iframe, '/embed.html')
Parameters
iframe
HTMLIFrameElement
The iframe element.
url
string
The URL to set as src.
Returns
void
reloadIframe()
reloadIframe(
iframe):void
Defined in: iframe/index.ts:99
Reloads the iframe content.
Example
import { reloadIframe } from '@rtorcato/browser-common/iframe'
reloadIframe(iframe)
Parameters
iframe
HTMLIFrameElement
The iframe element.
Returns
void
isIframeLoaded()
isIframeLoaded(
iframe):boolean
Defined in: iframe/index.ts:121
Returns true if the iframe is loaded (readyState is 'complete').
Example
import { isIframeLoaded } from '@rtorcato/browser-common/iframe'
if (isIframeLoaded(iframe)) handleReady()
Parameters
iframe
HTMLIFrameElement
The iframe element.
Returns
boolean
onIframeLoad()
onIframeLoad(
iframe,callback):void
Defined in: iframe/index.ts:136
Adds a load event listener to the iframe.
Example
import { onIframeLoad } from '@rtorcato/browser-common/iframe'
onIframeLoad(iframe, () => console.log('loaded'))
Parameters
iframe
HTMLIFrameElement
The iframe element.
callback
() => void
The function to call when loaded.
Returns
void