Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

iframe contentWindow and the load event

`contentWindow` gives you a reference to the iframe’s window, while `load` tells you that the browser has finished a load attempt. Neither confirms success or grants DOM access.

Version: ECMAScript 2024 and the current web platform; runtime support varies

Syntax

const frameWindow = iframe.contentWindow;
iframe.addEventListener("load", handler);
iframe.addEventListener("load", handler, { once: true });

HTMLIFrameElement.contentWindow/load pairs a property with an event; it is not a single JavaScript member. The read-only property iframe.contentWindow returns the frame’s WindowProxy, or null if there is no browsing context, as with a newly created iframe before insertion. Across navigations in the same browsing context, the proxy keeps its identity even when the underlying Window and Document change. Having a proxy does not mean the intended document is ready.

Register the load listener on the iframe element, not its window, before inserting the frame or starting the navigation you want to observe. A later navigation can fire it again; { once: true } removes the listener just before invoking it for the first time. load can also fire when loading fails, and the iframe element does not fire error to report that failure. Neither event grants access or confirms that the embedded app is ready.

You can read iframe.contentWindow.document when the frame and parent are same-origin. Cross-origin access to that property throws SecurityError; iframe.contentDocument instead returns null. A sandbox without allow-same-origin can give the frame document an opaque origin. To communicate with a cooperating cross-origin frame whose origin you know, use postMessage() with an exact targetOrigin; the receiver should check event.origin, event.source, and the shape of the message data.

These are web-platform APIs, not ECMAScript 2024 features. The example needs a browser with a DOM; support and security policies depend on the environment.

Examples

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>iframe contentWindow and load</title>
</head>
<body>
  <script>
    (() => {
      const iframe = document.createElement("iframe");
      console.log(iframe.contentWindow === null);

      iframe.srcdoc = "<!doctype html><title>Frame ready</title>";
      iframe.addEventListener("load", () => {
        console.log(iframe.contentWindow.document.title);
        iframe.remove();
      }, { once: true });

      document.body.append(iframe);
    })();
  </script>
</body>
</html>

Result

true
Frame ready

Save the example as an HTML file, serve it over HTTP(S), and open it in a browser. The page must permit the inline script, the iframe, and the `srcdoc` assignment. The browser console shows `true` before insertion and `Frame ready` in the `load` handler. The example uses a fixed, trusted `srcdoc` string, fetches no external document, and removes the iframe after reading its title.

Related lessons

  • JavaScript · Lesson 5 · Advanced · Version: 4

Sources