Our volunteers haven't translated this article into Tiếng Việt yet. Join us and help get the job done!
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
interface and implements properties from WindowTimers
, WindowBase64
, and WindowEventHandlers
.
Note that properties which are objects (e.g.,. for overriding the prototype of built-in elements) are listed in a separate section below.
Window.applicationCache
Read only- An
OfflineResourceList
object providing access to the offline resources for the window. Window.caches
Read only- Returns the
CacheStorage
object associated with the current origin. This object enables service worker functionality such as storing assets for offline use, and generating custom responses to requests. Window.closed
Read only- This property indicates whether the current window is closed or not.
Window.Components
- The entry point to many XPCOM features. Some properties, e.g. classes, are only available to sufficiently privileged code. Web code should not use this property.
Window.console
Read only- Returns a reference to the console object which provides access to the browser's debugging console.
Window.content
and Window._content Read only- Returns a reference to the content element in the current window. The obsolete variant with underscore is no longer available from Web content.
Window.controllers
Read only- Returns the XUL controller objects for the current chrome window.
Window.crypto
Read only- Returns the browser crypto object.
Window.defaultStatus
Obsolete since Gecko 23- Gets/sets the status bar text for the given window.
Window.devicePixelRatio
Read only- Returns the ratio between physical pixels and device independent pixels in the current display.
Window.dialogArguments
Read only- Gets the arguments passed to the window (if it's a dialog box) at the time
window.showModalDialog()
was called. This is annsIArray
. Window.directories
- Synonym of
window.personalbar
Window.document
Read only- Returns a reference to the document that the window contains.
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
- This property indicates whether the window is displayed in full screen or not.
Window.globalStorage
Obsolete since 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
Read only- Returns a reference to the history object.
Window.innerHeight
- Gets the height of the content area of the browser window including, if rendered, the horizontal scrollbar.
Window.innerWidth
- Gets the width of the content area of the browser window including, if rendered, the vertical scrollbar.
Window.isSecureContext
Read only- Indicates whether a context is capable of using features that require secure contexts.
Window.length
Read only- Returns the number of frames in the window. See also
window.frames
. Window.location
Read only- Gets/sets the location, or current URL, of the window object.
Window.locationbar
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
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
Read only- The time in milliseconds since epoch at which the current animation cycle began.
Window.mozInnerScreenX
Read only- 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
innsIDOMWindowUtils
for a conversion factor to adapt to screen pixels if needed. Window.mozInnerScreenY
Read only- 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
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.opener
- Returns a reference to the window that opened this current window.
Window.orientation
Read only- Returns the orientation in degrees (in 90 degree increments) of the viewport relative to the device's natural orientation.
Window.outerHeight
Read only- Gets the height of the outside of the browser window.
Window.outerWidth
Read only- Gets the width of the outside of the browser window.
Window.pageXOffset
Read only- An alias for
window.scrollX
. Window.pageYOffset
Read only- An alias for
window.scrollY
Window.sessionStorage
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.parent
Read only- Returns a reference to the parent of the current window or subframe.
Window.performance
Read only- Provides a hosting area for performance related attributes.
Window.personalbar
Read only- Returns the personalbar object, whose visibility can be toggled in the window.
Window.pkcs11
Obsolete since 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()
to display the window as a modal dialog. Window.screen
Read only- Returns a reference to the screen object associated with the window.
Window.screenX
Read only- Returns the horizontal distance of the left border of the user's browser from the left side of the screen.
Window.screenY
Read only- Returns the vertical distance of the top border of the user's browser from the top side of the screen.
Window.scrollbars
Read only- Returns the scrollbars object, whose visibility can be toggled in the window.
Window.scrollMaxX
Read only- The maximum offset that the window can be scrolled to horizontally, that is the document width minus the viewport width.
Window.scrollMaxY
Read only- The maximum offset that the window can be scrolled to vertically (i.e., the document height minus the viewport height).
Window.scrollX
Read only- Returns the number of pixels that the document has already been scrolled horizontally.
Window.scrollY
Read only- Returns the number of pixels that the document has already been scrolled vertically.
Window.self
Read only- Returns an object reference to the window object itself.
Window.sessionStorage
- Returns a storage object for storing data within a single page session.
Window.sidebar
Read only- Returns a reference to the window object of the sidebar.
Window.speechSynthesis
Read only- Returns a
SpeechSynthesis
object, which is the entry point into using Web Speech API speech synthesis functionality. Window.status
- Gets/sets the text in the statusbar at the bottom of the browser.
Window.statusbar
Read only- Returns the statusbar object, whose visibility can be toggled in the window.
Window.toolbar
Read only- Returns the toolbar object, whose visibility can be toggled in the window.
Window.top
Read only- Returns a reference to the topmost window in the window hierarchy. This property is read only.
Window.window
Read only- Returns a reference to the current window.
window[0]
,window[1]
, etc.- Returns a reference to the
window
object in the frames. SeeWindow.frames
for more details.
Methods
This interface inherits methods from the EventTarget
interface and implements methods from WindowTimers
, WindowBase64
, WindowEventHandlers
, and GlobalFetch
.
EventTarget.addEventListener()
- Register an event handler to a specific event type on the window.
Window.alert()
- Displays an alert dialog.
WindowBase64.atob()
- Decodes a string of data which has been encoded using base-64 encoding.
Window.back()
- Moves back one in the window history.
Window.blur()
- Sets focus away from the window.
WindowBase64.btoa()
- Creates a base-64 encoded ASCII string from a string of binary data.
Window.cancelIdleCallback()
- Enables you to cancel a callback previously scheduled with
Window.requestIdleCallback
. Window.captureEvents()
- Registers the window to capture all events of the specified type.
Window.clearImmediate()
- Cancels the repeated execution set using
setImmediate
. WindowTimers.clearInterval()
- Cancels the repeated execution set using
WindowTimers.setInterval()
. WindowTimers.clearTimeout()
- Cancels the repeated execution set using
WindowTimers.setTimeout()
. Window.close()
- Closes the current window.
Window.confirm()
- Displays a dialog with a message that the user needs to respond to.
Window.disableExternalCapture()
Obsolete since Gecko 24- FIXME: NeedsContents
Window.dispatchEvent()
- Used to trigger an event.
Window.dump()
- Writes a message to the console.
Window.enableExternalCapture()
Obsolete since Gecko 24- FIXME: NeedsContents
GlobalFetch.fetch()
- Starts the process of fetching a resource.
Window.find()
- Searches for a given string in a window.
Window.focus()
- Sets focus on the current window.
Window.forward()
- Moves the window one document forward in the history.
Window.getAttention()
- Flashes the application icon.
Window.getAttentionWithCycleCount()
- FIXME: NeedsContents
Window.getComputedStyle()
- Gets computed style for the specified element. Computed style indicates the computed values of all CSS properties of the element.
Window.getDefaultComputedStyle()
- Gets default computed style for the specified element, ignoring author stylesheets.
Window.getSelection()
- Returns the selection object representing the selected item(s).
Window.home()
- Returns the browser to the home page.
Window.matchMedia()
- Returns a
MediaQueryList
object representing the specified media query string. Window.maximize()
- FIXME: NeedsContents
Window.minimize()
(top-level XUL windows only)- Minimizes the window.
Window.moveBy()
- Moves the current window by a specified amount.
Window.moveTo()
- Moves the window to the specified coordinates.
Window.mozRequestAnimationFrame()
- Tells the browser that an animation is in progress, requesting that the browser schedule a repaint of the window for the next animation frame. This will cause a
MozBeforePaint
event to fire before that repaint occurs. Window.open()
- Opens a new window.
Window.openDialog()
- Opens a new dialog window.
Window.postMessage()
- 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()
- Opens the Print Dialog to print the current document.
Window.prompt()
- Returns the text entered by the user in a prompt dialog.
Window.releaseEvents()
- Releases the window from trapping events of a specific type.
Window.removeEventListener()
- Removes an event listener from the window.
Window.requestIdleCallback()
- 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()
- FIXME: NeedsContents
Window.routeEvent()
Obsolete since Gecko 24- FIXME: NeedsContents
Window.scroll()
- Scrolls the window to a particular place in the document.
Window.scrollBy()
- Scrolls the document in the window by the given amount.
Window.scrollByLines()
- Scrolls the document by the given number of lines.
Window.scrollByPages()
- Scrolls the current document by the specified number of pages.
Window.scrollTo()
- Scrolls to a particular set of coordinates in the document.
Window.setCursor()
- Changes the cursor for the current window
Window.setImmediate()
- Executes a function after the browser has finished other heavy tasks
WindowTimers.setInterval()
- Schedules the execution of a function each X milliseconds.
Window.setResizable()
- Toggles a user's ability to resize a window.
WindowTimers.setTimeout()
- Sets a delay for executing a function.
Window.showModalDialog()
- Displays a modal dialog.
Window.sizeToContent()
- Sizes the window according to its content.
Window.stop()
- This method stops window loading.
Window.updateCommands()
- Updates the state of commands of the current chrome window (UI).
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
interface and implements event handlers from WindowTimers
, WindowBase64
, and WindowEventHandlers
.
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
- 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
- Called when the print dialog box is closed. See
afterprint
event. WindowEventHandlers.onbeforeprint
- Called when the print dialog box is opened. See
beforeprint
event. Window.onbeforeinstallprompt
- An event handler property dispatched before a user is prompted to save a web site to a home screen on mobile.
WindowEventHandlers.onbeforeunload
- 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
- An event handler property for change events on the window.
GlobalEventHandlers.onclick
- Called after the ANY mouse button is pressed & released
GlobalEventHandlers.ondblclick
- Called when a double click is made with ANY mouse button.
GlobalEventHandlers.onclose
- Called after the window is closed
GlobalEventHandlers.oncontextmenu
- Called when the RIGHT mouse button is pressed
Window.ondevicelight
- An event handler property for any ambient light levels changes
Window.ondevicemotion
- Called if accelerometer detects a change (For mobile devices)
Window.ondeviceorientation
- Called when the orientation is changed (For mobile devices)
Window.ondeviceorientationabsolute
Chrome only- An event handler property for any device orientation changes.
Window.ondeviceproximity
- An event handler property for device proximity event
GlobalEventHandlers.onerror
- Called when a resource fails to load OR when an error occurs at runtime. See
error
event. GlobalEventHandlers.onfocus
- Called after the window receives or regains focus. See
focus
events. WindowEventHandlers.onhashchange
- An event handler property for
hashchange
events on the window; called when the part of the URL after the hash mark ("#") changes. Window.oninstall
- Called when the page is installed as a webapp. See
install
event. Window.oninput
- Called when the value of an <input> element changes
GlobalEventHandlers.onkeydown
- Called when you begin pressing ANY key. See
keydown
event. GlobalEventHandlers.onkeypress
- Called when a key (except Shift, Fn, and CapsLock) is in pressed position. See
keypress
event. GlobalEventHandlers.onkeyup
- Called when you finish releasing ANY key. See
keyup
event. WindowEventHandlers.onlanguagechange
- An event handler property for
languagechange
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
- Is an
EventHandler
representing the code to be called when themessage
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
- Called when the pointer leaves the window.
GlobalEventHandlers.onmouseover
- Called when the pointer enters the window
GlobalEventHandlers.onmouseup
- Called when ANY mouse button is released
Window.onmozbeforepaint
- 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 theWindow.mozRequestAnimationFrame()
method. WindowEventHandlers.onoffline
- Called when network connection is lost. See
offline
event. WindowEventHandlers.ononline
- Called when network connection is established. See
online
event. WindowEventHandlers.onpagehide
- Called when the user navigates away from the page, before the onunload event. See
pagehide
event. WindowEventHandlers.onpageshow
- Called after all resources and the DOM are fully loaded. See
pageshow
event. Window.onpaint
- An event handler property for paint events on the window.
WindowEventHandlers.onpopstate
- Called when a back putton is pressed.
Window.onrejectionhandled
- An event handler for handled
Promise
rejection events. GlobalEventHandlers.onreset
- Called when a form is reset
GlobalEventHandlers.onresize
- Called continuously as you are resizing the window.
GlobalEventHandlers.onscroll
- 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
- Called when the mouse wheel is rotated around any axis
GlobalEventHandlers.onselect
- Called after text in an input field is selected
GlobalEventHandlers.onselectionchange
- Is an
EventHandler
representing the code to be called when theselectionchange
event is raised. WindowEventHandlers.onstorage
- Called when there is a change in session storage or local storage. See
storage
event GlobalEventHandlers.onsubmit
- Called when a form is submitted
WindowEventHandlers.onunhandledrejection
- An event handler for unhandled
Promise
rejection events. WindowEventHandlers.onunload
- Called when the user navigates away from the page.
Window.onuserproximity
- An event handler property for user proximity events.
Window.onvrdisplayconnected
- Represents an event handler that will run when a compatible VR device has been connected to the computer (when the
vrdisplayconnected
event fires). Window.onvrdisplaydisconnected
- Represents an event handler that will run when a compatible VR device has been disconnected from the computer (when the
vrdisplaydisconnected
event fires). Window.onvrdisplaypresentchange
- 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
onvrdisplaypresentchange
event fires).
Constructors
See also the DOM Interfaces.
Window.DOMParser
- FIXME: NeedsContents
Window.GeckoActiveXObject
- FIXME: NeedsContents
Image
- Used for creating an
HTMLImageElement
. Option
- Used for creating an
HTMLOptionElement
Window.QueryInterface
- FIXME: NeedsContents
Window.XMLSerializer
- FIXME: NeedsContents
Worker
- Used for creating a Web worker
Window.XPCNativeWrapper
- FIXME: NeedsContents
Window.XPCSafeJSObjectWrapper
- FIXME: NeedsContents
Interfaces
See DOM Reference