Lesson 0002 · ~10 minutes
One skill: name the
WindowProxy
for the window you want. Then you can send data with
postMessage.
You can say this: “The iframe element is not the handle.
contentWindow is the handle. Inside a child I use
parent or top. In a popup I use
opener.”
You cannot send data to a window if you do not hold it.
A WindowProxy is that hold.
You get a WindowProxy in a small set of ways.
This lesson teaches those ways. It does not add new
postMessage rules.
| You want | You write |
|---|---|
| The window inside an iframe element |
iframe.contentWindow
|
| The page that embeds this page |
window.parent
|
| The outermost page |
window.top
|
| The page that opened this popup |
window.opener
|
window.open(...) returns the WindowProxy of the new
window
(MDN). Store that value.
event.source is also a WindowProxy. That is the next
lesson.
If this page is
not in an iframe,
parent is this window.
top is this window.
(MDN parent)
If this page is in one iframe,
parent and top are the same handle.
If this page is in a nested iframe, they are not the same handle.
this page parent === this window
top === this window
|
| iframe.contentWindow
v
mid iframe parent === this page
top === this page
|
| iframe.contentWindow
v
inner iframe parent === mid
top === this page
From inner, parent is mid. top is this page.
Pick parent when you want the embedder.
Pick top when you want the outermost page.
window.open makes a new window. The return value is
the handle of that window.
Inside the new window,
opener
is the page that called open
(MDN).
opener is not parent.
A popup is not inside an iframe. Its parent is itself.
If you pass noopener, open returns
null. The new window has no opener. You
have no handle.
(MDN)
If the popup is closed, you can still hold the handle.
handle.closed is true.
const iframeRef = useRef(null);
function sendFilter(q) {
const otherWindow = iframeRef.current.contentWindow;
otherWindow.postMessage(
{ type: "set-filter", q },
"https://widget.example",
);
}
return <iframe ref={iframeRef} src="https://widget.example" />;
The ref is the iframe element.
contentWindow is the WindowProxy.
The box below has a real mid iframe. Mid has a real inner iframe.
parent===self true,
top===self true.
parent===self false,
parent===top false.
iframe.contentWindow.
mid.frames[0]. That is the inner WindowProxy.
opener is null: false. Close the popup. Click
Is popup closed?
Optional: click Open with noopener. The call must
return null. You have no handle.
Do the five steps first. Then answer these questions.
You have an iframe element. You need the child's WindowProxy.
You are inside a nested iframe. You need the page that embeds you.
You are in the innermost iframe. You need the outermost page.
You are in a popup. You need the page that opened you.
You are not in an iframe. You read window.parent.
contentWindow is.
parent is one step out.
top is the outermost page.
parent is this window.
opener is the page that called
window.open. It is not parent.
window.open returns. Check
closed. noopener means no handle.
MDN — Window.parent and MDN — Window.open(). Then contentWindow and opener.