files/en-us/web/api/window/index.md
{{APIRef("DOM")}}
The Window interface represents a window containing a {{glossary("DOM")}} document; the document property points to the DOM document loaded in that window.
A window for a given document can be obtained using the {{domxref("document.defaultView")}} property.
A global variable, window, representing the window in which the script is running, is exposed to JavaScript code.
The Window interface is home to a variety of functions, namespaces, objects, and constructors which are not necessarily directly associated with the concept of a user interface window. However, the Window interface is a suitable place to include these items that need to be globally available. Many of these are documented in the JavaScript Reference and the DOM Reference.
In a tabbed browser, each tab is represented by its own Window object; the global window seen by JavaScript code running within a given tab always represents the tab in which the code is running. That said, even in a tabbed browser, some properties and methods still apply to the overall window that contains the tab, such as {{Domxref("Window.resizeTo", "resizeTo()")}} and {{Domxref("Window.innerHeight", "innerHeight")}}. Generally, anything that can't reasonably pertain to a tab pertains to the window instead.
{{InheritanceDiagram}}
This interface inherits properties from the {{domxref("EventTarget")}} interface.
Note that properties which are objects (e.g., for overriding the prototype of built-in elements) are listed in a separate section below.
true) or not (false).launch_handler client_mode value of focus-existing, navigate-new, or navigate-existing, the launchQueue provides access to the {{domxref("LaunchQueue")}} class, which allows custom launch navigation handling to be implemented for the PWA.mozScreenPixelsPerCSSPixel in nsIDOMWindowUtils for a conversion factor to adapt to screen pixels if needed.mozScreenPixelsPerCSSPixel for a conversion factor to adapt to screen pixels if needed.window's associated {{domxref("Navigation")}} object. The entry point for the Navigation API.true if this window belongs to an origin-keyed agent cluster.window[0], window[1], etc.
window object in the frames. See {{domxref("Window.frames")}} for more details.: Some elements in the document are also exposed as window properties:
name (if non-empty) is exposed.
For example, if the document contains <form name="my_form">, then window["my_form"] (and its equivalent window.my_form) returns a reference to that element.id (if non-empty) is exposed.If a property corresponds to a single element, that element is directly returned. If the property corresponds to multiple elements, then an {{domxref("HTMLCollection")}} is returned containing all of them. If any of the elements is a navigable <iframe> or <object>, then the {{domxref("HTMLIFrameElement/contentWindow", "contentWindow")}} of first such iframe is returned instead.
undefined if no event is currently being handled. The {{domxref("Event")}} object passed directly to event handlers should be used instead whenever possible.This interface inherits methods from the {{domxref("EventTarget")}} interface.
activateAfter timeout — whichever comes first.setImmediate().Listen to these events using addEventListener() or by assigning an event listener to the oneventname property of this interface. In addition to the events listed below, many events can bubble from the {{domxref("Document")}} contained in the window object.
localStorage or sessionStorage) has been modified in the context of another document.navigator.onLine has switched to false.navigator.onLine has switched to true.# symbol).Window object receives a message that can't be deserialized.Not all events that bubble can reach the Window object. Only the following do and can be listened for on the Window object:
abortcancelSee DOM Reference.
HTML elements have three ways to listen for events:
oneventname property in JavaScript.on-prefixed attribute to the element in the HTML.To listen for events on Window objects, in general, you can only use the first two methods, because Window has no corresponding HTML element. However, there's a specific group of events whose listeners can be added to the {{HTMLElement("body")}} (or the deprecated {{HTMLElement("frameset")}}) element that's owned by the Window's document, using the second or third methods. These events are:
afterprintbeforeprintbeforeunloadblurerrorfocushashchangelanguagechangeloadmessagemessageerrorofflineonlinepagehidepagerevealpageshowpageswappopstaterejectionhandledresizescrollstorageunhandledrejectionunloadThis means the following are strictly equivalent:
window.onresize = (e) => console.log(e.currentTarget);
document.body.onresize = (e) => console.log(e.currentTarget);
<body onresize="console.log(event.currentTarget)"></body>
In all three cases, you see the Window object logged as currentTarget.
{{Specifications}}
{{Compat}}