CKTIS / JavaScript
Window.parent, Window.self, and Window.origin
Distinguish references to the current and parent windows from a serialized origin, and learn why having a window reference does not grant access to its document.
Version: ECMAScript 2024 and the current web platform; runtime support varies
Syntax
window.parent
window.self
window.originThe label Window.parent/self/origin groups three properties, not a single API. window.self returns the current browsing context’s WindowProxy; normally window.self === window. In a frame, window.parent refers to the immediate parent, not necessarily window.top. On an active top-level page it refers to that page itself. The parent getter can return null if its Window is no longer associated with a navigable’s active document.
On an active page, window.parent !== window.self detects embedding, not an origin difference, provided neither native getter (parent or self) has been shadowed. A WindowProxy retains its identity in the same browsing context even when navigation changes the Window it forwards operations to.
window.origin returns the global environment’s origin as a string. For HTTP(S), the scheme, host, and port define the origin; the default port is omitted from the string. An opaque origin serializes to "null", not the value null. Two such strings do not establish a shared origin. location.origin instead describes the URL’s origin: an unsandboxed frame’s initial about:blank can inherit the parent’s origin even though its URL origin is "null".
You can obtain window.parent even across origins, but reads such as window.parent.document or window.parent.origin are normally blocked with a DOMException named SecurityError. For messaging, use postMessage with an exact targetOrigin when the recipient’s origin can be specified. On receipt, check event.origin, the expected event.source, and the data format. Do not trust "null" alone as proof of sender identity.
These are web platform APIs, not part of ECMAScript 2024. Web IDL marks all three properties readonly and [Replaceable]: a same-origin assignment can shadow a getter with an own property but cannot change window relationships or the security origin. In a Web Worker, self is the WorkerGlobalScope and self.origin describes the worker; there is no Window.parent there.
Examples
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Window.parent, Window.self, and Window.origin</title>
</head>
<body>
<script>
(() => {
const frame = document.createElement("iframe");
frame.addEventListener("load", () => {
const child = frame.contentWindow;
console.log(JSON.stringify({
self: child.self === child,
parent: child.parent === window.self,
embedded: child.parent !== child.self,
inheritedOrigin: child.origin === window.origin,
urlOrigin: child.location.origin
}));
frame.remove();
}, { once: true });
document.body.append(frame);
})();
</script>
</body>
</html>Result
{"self":true,"parent":true,"embedded":true,"inheritedOrigin":true,"urlOrigin":"null"}Open this HTML in a browser as a top-level HTTPS page without sandboxing or a policy blocking the inline script or the blank frame. The frame inherits the page’s origin, but the origin of its `about:blank` URL is `"null"`. Read the JSON line in the browser console after the `load` event.
Related lessons
- JavaScript · Lesson 5 · Advanced · Version: 4
Sources
- HTML Living Standard — The Window object
- HTML Living Standard — Accessing related windows
- HTML Living Standard — The WindowProxy exotic object
- HTML Living Standard — The WindowOrWorkerGlobalScope mixin
- HTML Living Standard — Origins
- HTML Living Standard — The WorkerGlobalScope common interface
- HTML Living Standard — Cross-document messaging
- HTML Living Standard — Security infrastructure for Window, WindowProxy, and Location objects
- HTML Living Standard — The Location interface