Skip to main content

@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:55

Posts a message to the iframe's contentWindow.

Example​

import { postMessageToIframe } from '@rtorcato/browser-common/iframe'
postMessageToIframe(iframe, { type: 'ping' }, 'https://example.com')

Parameters​

iframe​

HTMLIFrameElement

The iframe element.

message​

unknown

The message to send.

targetOrigin?​

string = '*'

The target origin (default: '*').

Returns​

void


setIframeSrc()​

setIframeSrc(iframe, url): void

Defined in: iframe/index.ts:76

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:89

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:111

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:126

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