Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

HTMLIFrameElement: contentWindow and the load event

HTMLIFrameElement represents an embedded frame. Its contentWindow property exposes the frame's window, while load lets you react when loading finishes. Document access depends on the same-origin policy, and load alone does not confirm a successful fetch.

Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support

Syntax

iframe.contentWindow
iframe.addEventListener("load", handler);
iframe.srcdoc = trustedMarkup;

HTMLIFrameElement is the DOM interface for an iframe element, not an ECMAScript language feature. “Iframe contentWindow and load” combines access to a frame's window with handling its loading. This is a browser API; having Node 24 does not establish its availability. The core interface and contentWindow are widely supported, but additional properties need separate compatibility checks.

The read-only contentWindow property exposes the embedded context's window through a WindowProxy. The same-origin policy restricts access: reading contentWindow.document is permitted for a same-origin frame, but throws an exception for a cross-origin frame. Use postMessage for cross-origin communication, checking the expected event.origin and event.source at the receiver.

Register load on the frame itself before starting loading or attaching it to the document. The event does not bubble and cannot be canceled. Do not treat it as proof of a successful fetch: frames fire load even when loading fails and do not fire a corresponding error. The { once: true } option handles only the first event, not subsequent navigations.

The example assigns a fixed document through srcdoc, reads its text after load, and removes the frame. Without sandbox restrictions, that document is same-origin with its parent. srcdoc interprets HTML: do not pass untrusted data into it. CSP or a TrustedHTML requirement may block the example.

Examples

const iframe = document.createElement("iframe");
iframe.title = "Demo";
iframe.addEventListener("load", () => {
  const childWindow = iframe.contentWindow;
  console.log(childWindow.document.querySelector("p").textContent);
  iframe.remove();
}, { once: true });
iframe.srcdoc = '<!doctype html><html lang="en"><title>Demo</title><body><p>Ready</p></body></html>';
document.body.append(iframe);

Result

Ready

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources