This page is Ready to Use

Notice: The WebPlatform project, supported by various stewards between 2012 and 2015, has been discontinued. This site is now available on github.

Window

Summary

The window object is the top level Javascript object in a page, and is the global scope for a browser tab. Global Javascript variables appear in the window object, as well as several important objects such as Document.

Inherits from EventTargetEventTarget

Properties

URL
Sets or gets the URL for the current document.
XMLHttpRequest
Represents an XML request using HTTP.
animationStartTime
Obsolete. Returns a timestamp of the start time of the current refresh interval, such that multiple animations can be synchronized with each other.
closed
This read-only property indicates whether the referenced window is closed or not.
defaultStatus
Sets or retrieves the default message displayed in the status bar at the bottom of the window.
frames
Returns the window itself, which is an array-like object, listing the direct sub-frames of the current window.
indexedDB
Provides access to the IndexedDB features supported by the browser and/or device.
innerHeight
Height (in pixels) of the browser window viewport including, if rendered, the horizontal scrollbar.

onLine
:

screenLeft
Retrieves the x-coordinate of the upper left-hand corner of the window frame, relative to the upper left-hand corner of the screen.
screenTop
Retrieves the y-coordinate of the top corner of the client area, relative to the top corner of the screen.
status
Sets the text in the status bar at the bottom of the browser or returns the previously set text.
styleMedia
Gets a StyleMedia object that contains methods and properties. These methods and properties determine the media types that are supported by the object that displays the document object.
top
Retrieves the topmost ancestor window.

Methods

alert
Displays a synchronized dialog box showing the given text and a localized OK button.
cancelAnimationFrame
Cancels a requestAnimationFrame request
clearImmediate
Cancels a function request created with setImmediate.
clearInterval
Cancels the interval previously started using the setInterval method.
clearTimeout
Cancels a time-out that was set with the setTimeout method.
close
Closes the current browser window or tab, or HTML Application (HTA).
confirm
Displays a synchronized confirmation dialog box showing the given text and possibly localized OK and Cancel buttons.
getComputedStyle
Gets the values of all the CSS properties of an element after applying the active stylesheets and resolving the basic computations they may contain. The returned object is of the same type that the object returned from the element’s “style” property, however the two objects have different purposes. The object returned from getComputedStyle is read-only and can be used to inspect the element’s style (including those set by a <style> element or an external stylesheet). The elt.style object should be used to set styles on a specific element.
getSelection
Returns a Selection object that represents the current selection of the document.
moveBy
Moves the screen position of the window by the specified x and y offset values.
moveRow
Moves a table row to a new position.
moveTo
Moves the screen position of the upper-left corner of the window to the specified x and y position
open
Opens a new window and loads the document specified by a given URL.
postMessage
Sends a cross-document message.
requestAnimationFrame
A method to invoke at the optimal time a callback to update the frame of an animation.
resizeBy
Changes the current size of the window by the specified x- and y-offset.
resizeTo
Sets the size of the window to the specified width and height values.
scroll
Causes the window to scroll to the specified x- and y-offset at the upper-left corner of the window.
scrollBy
Causes the window to scroll relative to the current scrolled position by the specified x- and y-pixel offset.
scrollTo
Scrolls the window to the specified x- and y-offset.
setImmediate
Requests that a function be called when current or pending tasks are complete, such as events or screen updates.
setInterval
Evaluates an expression each time a specified number of milliseconds has elapsed.
setTimeout
Evaluates an expression after a specified number of milliseconds has elapsed.
showModalDialog
Do not use. Use <dialog> or a popup window instead. Halts the script execution, creates a popup window, passes it parameters and returns a value when the new window is closed.

Events

message
Fires when a message is received from another context (frame, window, worker and similar).

Inherited from EventTarget

Properties

No properties.

Methods

addEventListener
Registers an event handler for the specified event type.
dispatchEvent
Sends an event to the current element.
removeEventListener
Removes an event handler that the addEventListener method registered.

Events

No events.

Examples

This example displays an alert for the current window.

alert("A simple message.")
if ( window.frames != null ) {
    for ( i = 0; i < window.frames.length; i++ )
        console.log("Child window " + i + " is named " + window.frames(i).name);
}

This example shows a simple event handler function for the window’s onload event. In the absence of a window element, the body element hosts the following window object events: onblur, onbeforeunload, onfocus, onload, and onunload.

<body onload="console.log('Document is loaded!');">

Notes

You can use the window object to retrieve information about the state of the window. You also can use this object to gain access to the document in the window, to the events that occur in the window, and to other factors that affect the window. You can apply any window property, method, or collection to any variable or expression that evaluates to a window object, regardless of how that window was created. Additionally, you can access all window properties, methods, and collections in the current window by using the property, method, or collection name directly—that is, without prefixing it with an expression that evaluates to the current window object. However, to help make more readable code and to avoid potential ambiguities, many authors use the window keyword when accessing window properties, methods, and collections for the current window. This keyword always refers to the current window. Note The window’s properties, methods, and collection names are reserved keywords and cannot be used as the names of variables or routines. The following table lists pertinent information for some of the properties of the window object.

PropertyMethodDescription
openeropenThe opener property is available only from a document opened using the window.open method.
parent, topNoneThe parent and top properties are available for a window opened inside a frame or iframe. The two properties return the topmost parent and immediate parent, respectively.
parent, topopenThe parent and top properties are available for a window opened via the open method or as a dialog and returns the current window.
lengthNoneRegardless of how the window is opened, the length property returns the number of frames in a window.
dialogArguments, dialogHeight, dialogLeft, dialogTop, dialogWidth, returnValueshowModalDialog and showModelessDialogThese properties are available only for windows created using the two methods listed, showModalDialog and showModelessDialog

Typically, the browser creates one window object when it opens an HTML document. However, if a document defines one or more frames (that is, contains one or more frame or iframe tags), the browser creates one window object for the original document and one additional window object for each frame. These additional objects are of the original window and can be affected by actions that occur in the original. For example, closing the original window causes all child windows to close. You can also create new windows (and corresponding window objects) using methods such as open, showModalDialog, and showModelessDialog.

Attributions