Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

Window — the browser document context

Window exposes browser interfaces to a page: pixel information, relationships between windows, and resize and navigation events. Learn how Window differs from WindowProxy and why cross-origin access remains restricted.

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

window.devicePixelRatio; window.self; window.parent; window.origin; window.addEventListener("resize", handler); window.addEventListener("popstate", handler); window.addEventListener("hashchange", handler);

Window is a browser document's global object, not part of ECMAScript itself. Access through window uses a WindowProxy, which forwards operations to the current Window. These interfaces are not available as window in workers; Node 24 being present in a repository does not establish browser support.

Window.self (window.self) refers to the same WindowProxy as window. Window.parent refers to the frame's parent window; in a top-level document it refers to the window itself. Obtaining a reference to a parent does not grant permission to read its DOM: cross-origin access is restricted. Window.origin returns the serialized origin of the global environment, usually a scheme, host, and optional port; an opaque origin produces the string "null", not the value null. It is neither a complete URL nor an authentication secret.

Window.devicePixelRatio is the ratio of CSS pixel size to device pixel size, useful for tasks such as scaling a canvas bitmap. Do not assume 1 or an integer: the result depends on the display environment and page zoom. CSSOM View, not ECMAScript, specifies this property's details.

Window.resize, Window.popstate, and Window.hashchange name events, not methods. Register them with addEventListener; corresponding handler properties are onresize, onpopstate, and onhashchange. resize signals a viewport size change. popstate accompanies certain changes to the active history entry, such as Back/Forward traversal; event.state exposes the entry's state. hashchange signals a URL fragment change and exposes oldURL and newURL. Calls to history.pushState() and history.replaceState() themselves emit neither navigation event, even when they change the fragment.

The example deterministically checks self and registration of three listeners. A synthetic Event changes neither window size, URL, nor history and lacks the specialized fields of PopStateEvent or HashChangeEvent. Its isTrusted is false; the example does not test real navigation.

Examples

(() => {
  console.log(`self: ${window.self === window}`);
  for (const type of ["resize", "popstate", "hashchange"]) {
    window.addEventListener(type, (event) => {
      console.log(`${event.type}: ${event.isTrusted}`);
    }, { once: true });
    window.dispatchEvent(new Event(type));
  }
})();

Result

self: true
resize: false
popstate: false
hashchange: false

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