@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.
Parameters
el
unknown
The element to check.
Returns
el is HTMLIFrameElement
Example
import { isIframe } from '@rtorcato/browser-common/iframe'
if (isIframe(el)) console.log(el.src)
getIframeWindow()
getIframeWindow(
iframe):Window|null
Defined in: iframe/index.ts:26
Returns the contentWindow of an iframe, or null if not available.
Parameters
iframe
HTMLIFrameElement
The iframe element.
Returns
Window | null
Example
import { getIframeWindow } from '@rtorcato/browser-common/iframe'
const win = getIframeWindow(iframe)
getIframeDocument()
getIframeDocument(
iframe):Document|null
Defined in: iframe/index.ts:40
Returns the contentDocument of an iframe, or null if not available.
Parameters
iframe
HTMLIFrameElement
The iframe element.
Returns
Document | null
Example
import { getIframeDocument } from '@rtorcato/browser-common/iframe'
const doc = getIframeDocument(iframe)
postMessageToIframe()
postMessageToIframe(
iframe,message,targetOrigin?):void
Defined in: iframe/index.ts:55
Posts a message to the iframe's contentWindow.
Parameters
iframe
HTMLIFrameElement
The iframe element.
message
unknown
The message to send.
targetOrigin?
string = '*'
The target origin (default: '*').
Returns
void
Example
import { postMessageToIframe } from '@rtorcato/browser-common/iframe'
postMessageToIframe(iframe, { type: 'ping' }, 'https://example.com')
setIframeSrc()
setIframeSrc(
iframe,url):void
Defined in: iframe/index.ts:76
Sets the src of an iframe safely.
Parameters
iframe
HTMLIFrameElement
The iframe element.
url
string
The URL to set as src.
Returns
void
Example
import { setIframeSrc } from '@rtorcato/browser-common/iframe'
setIframeSrc(iframe, '/embed.html')
reloadIframe()
reloadIframe(
iframe):void
Defined in: iframe/index.ts:89
Reloads the iframe content.
Parameters
iframe
HTMLIFrameElement
The iframe element.
Returns
void
Example
import { reloadIframe } from '@rtorcato/browser-common/iframe'
reloadIframe(iframe)
isIframeLoaded()
isIframeLoaded(
iframe):boolean
Defined in: iframe/index.ts:111
Returns true if the iframe is loaded (readyState is 'complete').
Parameters
iframe
HTMLIFrameElement
The iframe element.
Returns
boolean
Example
import { isIframeLoaded } from '@rtorcato/browser-common/iframe'
if (isIframeLoaded(iframe)) handleReady()
onIframeLoad()
onIframeLoad(
iframe,callback):void
Defined in: iframe/index.ts:126
Adds a load event listener to the iframe.
Parameters
iframe
HTMLIFrameElement
The iframe element.
callback
() => void
The function to call when loaded.
Returns
void
Example
import { onIframeLoad } from '@rtorcato/browser-common/iframe'
onIframeLoad(iframe, () => console.log('loaded'))