window

The window object represents a window containing a 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 document.defaultView property.

This section provides a brief reference for all of the methods, properties, and events available through the DOM window object. The window object implements the Window interface, which in turn inherits from the AbstractView interface. Some additional global functions, namespaces, objects, interfaces, and constructors, not typically associated with the window, but available on it, are listed in the JavaScript Reference and DOM Reference.

In a tabbed browser, such as Firefox, each tab contains its own window object (and if you're writing an extension, the browser window itself is a separate window too - see Working with windows in chrome code for more information). That is, the window object is not shared between tabs in the same window. Some methods, namely window.resizeTo and window.resizeBy apply to the whole window and not to the specific tab the window object belongs to. Generally, anything that can't reasonably pertain to a tab pertains to the window instead.

Properties

This interface inherits properties from the EventTarget (en-US) interface and implements properties from the WindowOrWorkerGlobalScope and WindowEventHandlers (en-US) mixins.

Note that properties which are objects (e.g.,. for overriding the prototype of built-in elements) are listed in a separate section below.

Window.closed This API has not been standardized. Read only
This property indicates whether the current window is closed or not.
Window.console (en-US) Read only
Returns a reference to the console object which provides access to the browser's debugging console.
Window.content and Window._content This API has not been standardized. This is an obsolete API and is no longer guaranteed to work. Read only
Returns a reference to the content element in the current window. Since Firefox 57 (initially Nightly-only), both versions are only available from chrome (privileged) code, and not available to the web anymore.
Window.controllers This API has not been standardized. Read only
Returns the XUL controller objects for the current chrome window.
Window.customElements (en-US)Read only
returns a reference to the CustomElementRegistry (en-US) object, which can be used to register new custom elements and get information about previously registered custom elements.
Window.crypto Read only
Returns the browser crypto object.
Window.defaultStatus Przestarzały Gecko 23
Gets/sets the status bar text for the given window.
Window.devicePixelRatio (en-US) This API has not been standardized. Read only
Returns the ratio between physical pixels and device independent pixels in the current display.
Window.dialogArguments (en-US) Read only
Gets the arguments passed to the window (if it's a dialog box) at the time window.showModalDialog() (en-US) was called. This is an nsIArray.
Window.directories This is an obsolete API and is no longer guaranteed to work.
Synonym of window.personalbar (en-US)
Window.document Read only
Returns a reference to the document that the window contains.
Window.DOMMatrix Read only This is an experimental API that should not be used in production code.
Returns a reference to a DOMMatrix (en-US) object, which represents 4x4 matrices, suitable for 2D and 3D operations.
Window.DOMMatrixReadOnly Read only This is an experimental API that should not be used in production code.
Returns a reference to a DOMMatrixReadOnly (en-US) object, which represents 4x4 matrices, suitable for 2D and 3D operations.
Window.DOMPoint Read only This is an experimental API that should not be used in production code.
Returns a reference to a DOMPoint (en-US) object, which represents a 2D or 3D point in a coordinate system.
Window.DOMPointReadOnly Read only This is an experimental API that should not be used in production code.
Returns a reference to a DOMPointReadOnly (en-US) object, which represents a 2D or 3D point in a coordinate system.
Window.DOMQuad Read only This is an experimental API that should not be used in production code.
Returns a reference to a DOMQuad (en-US) object, which provides represents a quadrilaterial object, that is one having four corners and four sides.
Window.DOMRect Read only This is an experimental API that should not be used in production code.
Returns a reference to a DOMRect (en-US) object, which represents a rectangle.
Window.DOMRectReadOnly Read only This is an experimental API that should not be used in production code.
Returns a reference to a DOMRectReadOnly (en-US) object, which represents a rectangle.
Window.frameElement Read only
Returns the element in which the window is embedded, or null if the window is not embedded.
Window.frames Read only
Returns an array of the subframes in the current window.
Window.fullScreen (en-US)
This property indicates whether the window is displayed in full screen or not.
Window.globalStorage This API has not been standardized. Przestarzały Gecko 13
Unsupported since Gecko 13 (Firefox 13). Use Window.localStorage instead.
Was: Multiple storage objects that are used for storing data across multiple pages.
Window.history (en-US) Read only
Returns a reference to the history object.
Window.innerHeight (en-US) Read only
Gets the height of the content area of the browser window including, if rendered, the horizontal scrollbar.
Window.innerWidth (en-US) Read only
Gets the width of the content area of the browser window including, if rendered, the vertical scrollbar.
Window.isSecureContext (en-US) Read only
Indicates whether a context is capable of using features that require secure contexts.
Window.length (en-US) Read only
Returns the number of frames in the window. See also window.frames.
Window.location (en-US)
Gets/sets the location, or current URL, of the window object.
Window.locationbar (en-US) Read only
Returns the locationbar object, whose visibility can be toggled in the window.
Window.localStorage Read only
Returns a reference to the local storage object used to store data that may only be accessed by the origin that created it.
Window.menubar (en-US) Read only
Returns the menubar object, whose visibility can be toggled in the window.
Window.messageManager
Returns the message manager object for this window.
Window.mozAnimationStartTime (en-US) Read only This deprecated API should no longer be used, but will probably still work.
The time in milliseconds since epoch at which the current animation cycle began.
Window.mozInnerScreenX (en-US) Read only This API has not been standardized.
Returns the horizontal (X) coordinate of the top-left corner of the window's viewport, in screen coordinates. This value is reported in CSS pixels. See mozScreenPixelsPerCSSPixel in nsIDOMWindowUtils for a conversion factor to adapt to screen pixels if needed.
Window.mozInnerScreenY (en-US) Read only This API has not been standardized.
Returns the vertical (Y) coordinate of the top-left corner of the window's viewport, in screen coordinates. This value is reported in CSS pixels. See mozScreenPixelsPerCSSPixel for a conversion factor to adapt to screen pixels if needed.
Window.mozPaintCount (en-US) This API has not been standardized. Read only
Returns the number of times the current document has been rendered to the screen in this window. This can be used to compute rendering performance.
Window.name
Gets/sets the name of the window.
Window.navigator Read only
Returns a reference to the navigator object.
Window.NetworkInformation Read only This is an experimental API that should not be used in production code.
Returns a reference to the NetworkInformation (en-US) interface, which provides information about the connection a device is using to communicate with the network and provides a means for scripts to be notified if the connection type changes.
Window.opener
Returns a reference to the window that opened this current window.
Window.orientation (en-US) This API has not been standardized. This deprecated API should no longer be used, but will probably still work. Read only
Returns the orientation in degrees (in 90 degree increments) of the viewport relative to the device's natural orientation.
Window.outerHeight (en-US) Read only
Gets the height of the outside of the browser window.
Window.outerWidth (en-US) Read only
Gets the width of the outside of the browser window.
Window.pageXOffset (en-US) Read only
An alias for window.scrollX (en-US).
Window.pageYOffset (en-US)Read only
An alias for window.scrollY (en-US)
Window.sessionStorage (en-US) Read only
Returns a reference to the session storage object used to store data that may only be accessed by the origin that created it.
Window.StaticRange (en-US) This is an experimental API that should not be used in production code. Read only
Returns a StaticRange() (en-US) constructor which creates a StaticRange (en-US) object.
Window.parent (en-US) Read only
Returns a reference to the parent of the current window or subframe.
Window.performance (en-US) Read only
Returns a Performance (en-US) object, which includes the timing (en-US) and navigation (en-US) attributes, each of which is an object providing performance-related data. See also Using Navigation Timing for additional information and examples.
Window.personalbar (en-US) Read only
Returns the personalbar object, whose visibility can be toggled in the window.
Window.pkcs11 (en-US) Przestarzały Gecko 29
Formerly provided access to install and remove PKCS11 modules.
Window.returnValue
The return value to be returned to the function that called window.showModalDialog() (en-US) to display the window as a modal dialog.
Window.screen (en-US) Read only
Returns a reference to the screen object associated with the window.
Window.screenX (en-US) Read only
Returns the horizontal distance of the left border of the user's browser from the left side of the screen.
Window.screenY (en-US) Read only
Returns the vertical distance of the top border of the user's browser from the top side of the screen.
Window.scrollbars (en-US) Read only
Returns the scrollbars object, whose visibility can be toggled in the window.
Window.scrollMaxX (en-US) This API has not been standardized. Read only
The maximum offset that the window can be scrolled to horizontally, that is the document width minus the viewport width.
Window.scrollMaxY (en-US) This API has not been standardized. Read only
The maximum offset that the window can be scrolled to vertically (i.e., the document height minus the viewport height).
Window.scrollX (en-US) Read only
Returns the number of pixels that the document has already been scrolled horizontally.
Window.scrollY (en-US) Read only
Returns the number of pixels that the document has already been scrolled vertically.
Window.self (en-US) Read only
Returns an object reference to the window object itself.
Window.sessionStorage (en-US)
Returns a storage object for storing data within a single page session.
Window.sidebar This API has not been standardized. Read only
Returns a reference to the window object of the sidebar.
Window.speechSynthesis (en-US) Read only
Returns a SpeechSynthesis (en-US) object, which is the entry point into using Web Speech API speech synthesis functionality.
Window.status (en-US)
Gets/sets the text in the statusbar at the bottom of the browser.
Window.statusbar (en-US) Read only
Returns the statusbar object, whose visibility can be toggled in the window.
Window.toolbar (en-US) Read only
Returns the toolbar object, whose visibility can be toggled in the window.
Window.top (en-US) Read only
Returns a reference to the topmost window in the window hierarchy. This property is read only.
Window.visualViewport (en-US) Read only
Returns a VisualViewport (en-US) object which represents the visual viewport for a given window.
Window.window (en-US) Read only
Returns a reference to the current window.
window[0], window[1], etc.
Returns a reference to the window object in the frames. See Window.frames for more details.

Properties implemented from elsewhere

WindowOrWorkerGlobalScope.caches (en-US) Read only
Returns the CacheStorage (en-US) object associated with the current context. This object enables functionality such as storing assets for offline use, and generating custom responses to requests.
WindowOrWorkerGlobalScope.indexedDB (en-US) Read only
Provides a mechanism for applications to asynchronously access capabilities of indexed databases; returns an IDBFactory (en-US) object.
WindowOrWorkerGlobalScope.isSecureContext (en-US) Read only
Returns a boolean indicating whether the current context is secure (true) or not (false).
WindowOrWorkerGlobalScope.origin (en-US) Read only
Returns the global object's origin, serialized as a string. (This does not yet appear to be implemented in any browser.)

Methods

This interface inherits methods from the EventTarget (en-US) interface and implements methods from WindowOrWorkerGlobalScope and EventTarget (en-US).

Window.alert()
Displays an alert dialog.
Window.back() (en-US) This API has not been standardized. This is an obsolete API and is no longer guaranteed to work.
Moves back one in the window history.
Window.blur() (en-US)
Sets focus away from the window.
Window.cancelAnimationFrame() (en-US) This is an experimental API that should not be used in production code.
Enables you to cancel a callback previously scheduled with Window.requestAnimationFrame.
Window.cancelIdleCallback() (en-US) This is an experimental API that should not be used in production code.
Enables you to cancel a callback previously scheduled with Window.requestIdleCallback (en-US).
Window.captureEvents() (en-US) This deprecated API should no longer be used, but will probably still work.
Registers the window to capture all events of the specified type.
Window.clearImmediate() (en-US)
Cancels the repeated execution set using setImmediate.
Window.close() (en-US)
Closes the current window.
Window.confirm() (en-US)
Displays a dialog with a message that the user needs to respond to.
Window.disableExternalCapture() Przestarzały Gecko 24
FIXME: NeedsContents
Window.dispatchEvent()
Used to trigger an event.
Window.dump() This API has not been standardized.
Writes a message to the console.
Window.enableExternalCapture() Przestarzały Gecko 24
FIXME: NeedsContents
Window.find() (en-US)
Searches for a given string in a window.
Window.focus()
Sets focus on the current window.
Window.forward() (en-US) This API has not been standardized. This is an obsolete API and is no longer guaranteed to work.
Moves the window one document forward in the history.
Window.getAttention() This API has not been standardized. This is an obsolete API and is no longer guaranteed to work.
Flashes the application icon.
Window.getAttentionWithCycleCount()
FIXME: NeedsContents
Window.getComputedStyle() (en-US)
Gets computed style for the specified element. Computed style indicates the computed values of all CSS properties of the element.
Window.getDefaultComputedStyle() (en-US) This API has not been standardized.
Gets default computed style for the specified element, ignoring author stylesheets.
Window.getSelection()
Returns the selection object representing the selected item(s).
Window.home() (en-US) This API has not been standardized. This is an obsolete API and is no longer guaranteed to work.
Returns the browser to the home page.
Window.matchMedia() (en-US)
Returns a MediaQueryList (en-US) object representing the specified media query string.
Window.maximize()
FIXME: NeedsContents
Window.minimize() (en-US) (top-level XUL windows only)
Minimizes the window.
Window.moveBy() (en-US)
Moves the current window by a specified amount.
Window.moveTo() (en-US)
Moves the window to the specified coordinates.
Window.open()
Opens a new window.
Window.openDialog() This API has not been standardized. This is an obsolete API and is no longer guaranteed to work.
Opens a new dialog window.
Window.postMessage() (en-US)
Provides a secure means for one window to send a string of data to another window, which need not be within the same domain as the first.
Window.print() (en-US)
Opens the Print Dialog to print the current document.
Window.prompt()
Returns the text entered by the user in a prompt dialog.
Window.releaseEvents() (en-US) This API has not been standardized. This deprecated API should no longer be used, but will probably still work.
Releases the window from trapping events of a specific type.
Window.requestAnimationFrame()
Tells the browser that an animation is in progress, requesting that the browser schedule a repaint of the window for the next animation frame.
Window.requestIdleCallback() (en-US) This is an experimental API that should not be used in production code.
Enables the scheduling of tasks during a browser's idle periods.
Window.resizeBy()
Resizes the current window by a certain amount.
Window.resizeTo()
Dynamically resizes window.
Window.restore() (en-US) This API has not been standardized. This is an obsolete API and is no longer guaranteed to work.
FIXME: NeedsContents
Window.routeEvent() (en-US) Przestarzały Gecko 24
FIXME: NeedsContents
Window.scroll()
Scrolls the window to a particular place in the document.
Window.scrollBy() (en-US)
Scrolls the document in the window by the given amount.
Window.scrollByLines() This API has not been standardized.
Scrolls the document by the given number of lines.
Window.scrollByPages() This API has not been standardized.
Scrolls the current document by the specified number of pages.
Window.scrollTo()
Scrolls to a particular set of coordinates in the document.
Window.setCursor() (en-US) This API has not been standardized. (top-level XUL windows only)
Changes the cursor for the current window
Window.setImmediate() (en-US)
Executes a function after the browser has finished other heavy tasks
Window.setResizable() This API has not been standardized.
Toggles a user's ability to resize a window.
Window.sizeToContent() (en-US) This API has not been standardized.
Sizes the window according to its content.
Window.stop() (en-US)
This method stops window loading.
Window.updateCommands() (en-US) This API has not been standardized.
Updates the state of commands of the current chrome window (UI).

Methods implemented from elsewhere

EventTarget.addEventListener()
Register an event handler to a specific event type on the window.
WindowOrWorkerGlobalScope.atob()
Decodes a string of data which has been encoded using base-64 encoding.
WindowOrWorkerGlobalScope.btoa()
Creates a base-64 encoded ASCII string from a string of binary data.
WindowOrWorkerGlobalScope.clearInterval()
Cancels the repeated execution set using WindowOrWorkerGlobalScope.setInterval().
WindowOrWorkerGlobalScope.clearTimeout()
Cancels the delayed execution set using WindowOrWorkerGlobalScope.setTimeout().
WindowOrWorkerGlobalScope.createImageBitmap() (en-US)
Accepts a variety of different image sources, and returns a Promise (en-US) which resolves to an ImageBitmap (en-US). Optionally the source is cropped to the rectangle of pixels originating at (sx, sy) with width sw, and height sh.
WindowOrWorkerGlobalScope.fetch() (en-US)
Starts the process of fetching a resource from the network.
EventTarget.removeEventListener (en-US)
Removes an event listener from the window.
WindowOrWorkerGlobalScope.setInterval()
Schedules a function to execute every time a given number of milliseconds elapses.
WindowOrWorkerGlobalScope.setTimeout()
Schedules a function to execute in a given amount of time.

Obsolete methods

Window.showModalDialog() (en-US) This is an obsolete API and is no longer guaranteed to work.
Displays a modal dialog. This method was removed completely in Chrome 43, and Firefox 55.

Event handlers

These are properties of the window object that can be set to establish event handlers for the various things that can happen in the window that might be of interest.

This interface inherits event handlers from the EventTarget (en-US) interface and implements event handlers from WindowEventHandlers (en-US).

Note: Starting in Gecko 9.0, you can now use the syntax if ("onabort" in window) to determine whether or not a given event handler property exists. This is because event handler interfaces have been updated to be proper web IDL interfaces. See DOM event handlers for details.

GlobalEventHandlers.onabort (en-US)
Called when the loading of a resource has been aborted, such as by a user canceling the load while it is still in progress
WindowEventHandlers.onafterprint (en-US)
Called when the print dialog box is closed. See afterprint (en-US) event.
WindowEventHandlers.onbeforeprint (en-US)
Called when the print dialog box is opened. See beforeprint (en-US) event.
Window.onbeforeinstallprompt (en-US)
An event handler property dispatched before a user is prompted to save a web site to a home screen on mobile.
WindowEventHandlers.onbeforeunload (en-US)
An event handler property for before-unload events on the window.
GlobalEventHandlers.onblur
Called after the window loses focus, such as due to a popup.
GlobalEventHandlers.onchange (en-US)
An event handler property for change events on the window.
GlobalEventHandlers.onclick
Called after the ANY mouse button is pressed & released
GlobalEventHandlers.ondblclick (en-US)
Called when a double click is made with ANY mouse button.
GlobalEventHandlers.onclose (en-US)
Called after the window is closed
GlobalEventHandlers.oncontextmenu (en-US)
Called when the RIGHT mouse button is pressed
Window.ondevicelight (en-US)
An event handler property for any ambient light levels changes
Window.ondevicemotion (en-US)
Called if accelerometer detects a change (For mobile devices)
Window.ondeviceorientation (en-US)
Called when the orientation is changed (For mobile devices)
Window.ondeviceorientationabsolute (en-US) This API has not been standardized. Chrome only
An event handler property for any device orientation changes.
Window.ondeviceproximity (en-US)
An event handler property for device proximity event
GlobalEventHandlers.onerror (en-US)
Called when a resource fails to load OR when an error occurs at runtime. See error (en-US) event.
GlobalEventHandlers.onfocus
Called after the window receives or regains focus. See focus (en-US) events.
WindowEventHandlers.onhashchange (en-US)
An event handler property for hashchange (en-US) events on the window; called when the part of the URL after the hash mark ("#") changes.
Window.onappinstalled (en-US)
Called when the page is installed as a webapp. See appinstalled event.
Window.ongamepadconnected (en-US)
Represents an event handler that will run when a gamepad is connected (when the gamepadconnected (en-US) event fires).
Window.ongamepaddisconnected (en-US)
Represents an event handler that will run when a gamepad is disconnected (when the gamepaddisconnected (en-US) event fires).
Window.oninput (en-US)
Called when the value of an <input> element changes
GlobalEventHandlers.onkeydown
Called when you begin pressing ANY key. See keydown (en-US) event.
GlobalEventHandlers.onkeypress
Called when a key (except Shift, Fn, and CapsLock) is in pressed position. See keypress (en-US) event.
GlobalEventHandlers.onkeyup
Called when you finish releasing ANY key. See keyup (en-US) event.
WindowEventHandlers.onlanguagechange (en-US)
An event handler property for languagechange (en-US) events on the window.
GlobalEventHandlers.onload
Called after all resources and the DOM are fully loaded. WILL NOT get called when the page is loaded from cache, such as with back button.
WindowEventHandlers.onmessage (en-US)
Is an EventHandler (en-US) representing the code to be called when the message event is raised.
GlobalEventHandlers.onmousedown
Called when ANY mouse button is pressed.
GlobalEventHandlers.onmousemove
Called continously when the mouse is moved inside the window.
GlobalEventHandlers.onmouseout (en-US)
Called when the pointer leaves the window.
GlobalEventHandlers.onmouseover (en-US)
Called when the pointer enters the window
GlobalEventHandlers.onmouseup (en-US)
Called when ANY mouse button is released
Window.onmozbeforepaint (en-US)
An event handler property for the MozBeforePaint event, which is sent before repainting the window if the event has been requested by a call to the Window.mozRequestAnimationFrame() (en-US) method.
WindowEventHandlers.onoffline
Called when network connection is lost. See offline (en-US) event.
WindowEventHandlers.ononline
Called when network connection is established. See online (en-US) event.
WindowEventHandlers.onpagehide
Called when the user navigates away from the page, before the onunload event. See pagehide (en-US) event.
WindowEventHandlers.onpageshow
Called after all resources and the DOM are fully loaded. See pageshow (en-US) event.
Window.onpaint (en-US)
An event handler property for paint events on the window.
WindowEventHandlers.onpopstate (en-US)
Called when a back button is pressed.
Window.onrejectionhandled (en-US) This is an experimental API that should not be used in production code.
An event handler for handled Promise rejection events.
GlobalEventHandlers.onreset (en-US)
Called when a form is reset
GlobalEventHandlers.onresize (en-US)
Called continuously as you are resizing the window.
GlobalEventHandlers.onscroll (en-US)
Called when the scroll bar is moved via ANY means. If the resource fully fits in the window, then this event cannot be invoked
GlobalEventHandlers.onwheel (en-US)
Called when the mouse wheel is rotated around any axis
GlobalEventHandlers.onselect (en-US)
Called after text in an input field is selected
GlobalEventHandlers.onselectionchange (en-US)
Is an EventHandler (en-US) representing the code to be called when the selectionchange event is raised.
WindowEventHandlers.onstorage (en-US)
Called when there is a change in session storage or local storage. See storage (en-US) event
GlobalEventHandlers.onsubmit (en-US)
Called when a form is submitted
WindowEventHandlers.onunhandledrejection (en-US) This is an experimental API that should not be used in production code.
An event handler for unhandled Promise rejection events.
WindowEventHandlers.onunload (en-US)
Called when the user navigates away from the page.
Window.onuserproximity (en-US)
An event handler property for user proximity events.
Window.onvrdisplayconnect (en-US)
Represents an event handler that will run when a compatible VR device has been connected to the computer (when the vrdisplayconnected event fires).
Window.onvrdisplaydisconnect (en-US)
Represents an event handler that will run when a compatible VR device has been disconnected from the computer (when the vrdisplaydisconnected event fires).
Window.onvrdisplayactivate (en-US)
Represents an event handler that will run when a display is able to be presented to (when the vrdisplayactivate event fires), for example if an HMD has been moved to bring it out of standby, or woken up by being put on.
Window.onvrdisplaydeactivate (en-US)
Represents an event handler that will run when a display can no longer be presented to (when the vrdisplaydeactivate event fires), for example if an HMD has gone into standby or sleep mode due to a period of inactivity.
Window.onvrdisplayblur (en-US)
Represents an event handler that will run when presentation to a display has been paused for some reason by the browser, OS, or VR hardware (when the vrdisplayblur event fires) — for example, while the user is interacting with a system menu or browser, to prevent tracking or loss of experience.
Window.onvrdisplayfocus (en-US)
Represents an event handler that will run when presentation to a display has resumed after being blurred (when the vrdisplayfocus event fires).
Window.onvrdisplaypresentchange (en-US)
represents an event handler that will run when the presenting state of a VR device changes — i.e. goes from presenting to not presenting, or vice versa (when the vrdisplaypresentchange event fires).

Constructors

See also the DOM Interfaces.

DOMParser (en-US)
DOMParser can parse XML or HTML source stored in a string into a DOM Document. DOMParser is specified in DOM Parsing and Serialization.
Window.GeckoActiveXObject
FIXME: NeedsContents
Image (en-US)
Used for creating an HTMLImageElement (en-US).
Option (en-US)
Used for creating an HTMLOptionElement (en-US)
Window.QueryInterface
FIXME: NeedsContents
Window.XMLSerializer
FIXME: NeedsContents
Worker (en-US)
Used for creating a Web worker
Window.XPCNativeWrapper
FIXME: NeedsContents
Window.XPCSafeJSObjectWrapper
FIXME: NeedsContents

Interfaces

See DOM Reference

See also