docs.rodeo

MDN Web Docs mirror

Window

{{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}} 

Instance properties

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.

Deprecated properties

Instance methods

This interface inherits methods from the {{domxref("EventTarget")}}  interface.

Deprecated methods

Events

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.

Clipboard events

Connection events

Device orientation events

Focus events

Gamepad events

History events

Load & unload events

Manifest events

Messaging events

Promise rejection events

Scroll events

Deprecated events

Bubbled events

Not all events that bubble can reach the Window object. Only the following do and can be listened for on the Window object:

Interfaces

See DOM Reference.

Listening for events on Window

HTML elements have three ways to listen for events:

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:

This 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

{{Specifications}} 

Browser compatibility

{{Compat}} 

In this article

View on MDN