Window
window
物件代表了一個包含 DOM 文件的視窗,其中的 document
屬性指向了視窗中載入的 Document
物件。而使用 document
的 defaultView
屬性,則可取得該 Document 物件所在的視窗 window
物件。
本節提供了 DOM window
物件的所有方法、屬性以及事件的說明。window
物件實作了 Window
介面,而 Window
介面則繼承自 AbstractView
介面。一些額外、與 window
物件沒有直接關係,但卻置於 window
物件中的全域函式、命名空間、物件、介面以及建構式,將會在 JavaScript 參考文件和文件物件模型(DOM)中說明。
在支援分頁的瀏覽器中(如 Firefox),每一個分頁標籤都擁有自己的 window
物件(如果你正在撰寫瀏覽器附加元件,瀏覽器視窗本身也是一個個別的 window-請參閱 Working with windows in chrome code),分頁不會於同一個瀏覽器視窗中共享 window
物件。但有部分的方法,如 window.resizeTo()
(en-US) 或 window.resizeBy()
(en-US) 會套用至整個視窗,而不只是該 window
物件所屬的分頁標籤。一般來說,若無法適當的應用於分頁,便會套用至瀏覽器視窗。
屬性
This interface inherits properties from the EventTarget
interface and implements properties from the WindowOrWorkerGlobalScope
and WindowEventHandlers
mixins.
請注意,指向物件的屬性(例如覆蓋了內建物件原型的元素)會在另外的列表中說明。
Window.applicationCache
(en-US) Read only-
指向
OfflineResourceList
物件,此物件提供了視窗的離線資源。 Window.closed
(en-US) 非標準 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
(en-US) Read only-
Returns a reference to the console object which provides access to the browser's debugging console.
Window.content
(en-US) 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
(en-US) Read only-
Returns the browser crypto object.
Window.defaultStatus
(en-US) 已棄用-
Gets/sets the status bar text for the given window.
Window.devicePixelRatio
(en-US) 非標準 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()
(en-US) was called. This is annsIArray
. Window.directories
已棄用-
Synonym of
window.personalbar
(en-US) Window.document
(en-US) Read only-
指向此
window
中的document
物件。 Window.frameElement
(en-US) Read only-
Returns the element in which the window is embedded, or null if the window is not embedded.
Window.frames
(en-US) Read only-
Returns an array of the subframes in the current window.
Window.fullScreen
(en-US)-
此屬性表示目前視窗是否為全螢幕顯示。
Window.globalStorage
非標準 已棄用-
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-
指向 history 物件。
Window.innerHeight
(en-US) Read only-
取得視窗內的網頁內容高度,包含水平捲軸。
Window.innerWidth
(en-US) Read only-
取得視窗內的網頁內容寬度,包含垂直捲軸。
Window.isSecureContext
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
(en-US). Window.location
-
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.
-
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 已棄用-
The time in milliseconds since epoch at which the current animation cycle began.
Window.mozInnerScreenX
(en-US) 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
(en-US) 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
(en-US) 非標準 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
(en-US)-
Gets/sets the name of the window.
-
Returns a reference to the navigator object.
Window.opener
-
Returns a reference to the window that opened this current window.
Window.orientation
(en-US) 非標準 已棄用 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-
取得瀏覽器視窗的高度。
Window.outerWidth
(en-US) Read only-
取得瀏覽器視窗的寬度。
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
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
(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
object, which includes thetiming
andnavigation
(en-US) attributes, each of which is an object providing performance-related (en-US) data. See also Using Navigation Timing (en-US) 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
已棄用-
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-
回傳一個與 window 關聯的 screen 物件。
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) 非標準 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) 非標準 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
-
Returns a storage object for storing data within a single page session.
-
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 (en-US) 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.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. SeeWindow.frames
(en-US) for more details.
Properties implemented from elsewhere
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. indexedDB
(en-US) Read only-
Provides a mechanism for applications to asynchronously access capabilities of indexed databases; returns an
IDBFactory
(en-US) object. isSecureContext
(en-US) Read only-
Returns a boolean indicating whether the current context is secure (
true
) or not (false
). 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.)
方法
This interface inherits methods from the EventTarget
interface and implements methods from WindowOrWorkerGlobalScope
and EventTarget
.
Window.alert()
(en-US)-
Displays an alert dialog.
Window.back()
(en-US) 非標準 已棄用-
Moves back one in the window history.
Window.blur()
(en-US)-
Sets focus away from the window.
Window.cancelAnimationFrame()
(en-US) 實驗性質-
Enables you to cancel a callback previously scheduled with
Window.requestAnimationFrame
. Window.cancelIdleCallback()
(en-US) 實驗性質-
Enables you to cancel a callback previously scheduled with
Window.requestIdleCallback
. Window.captureEvents()
(en-US) 已棄用-
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()
已棄用- {{todo("NeedsContents")}}
Window.dispatchEvent()
-
Used to trigger an event.
Window.dump()
(en-US) 非標準-
Writes a message to the console.
Window.enableExternalCapture()
已棄用- {{todo("NeedsContents")}}
Window.find()
(en-US)-
Searches for a given string in a window.
Window.focus()
(en-US)-
Sets focus on the current window.
Window.forward()
(en-US) 非標準 已棄用-
Moves the window one document forward in the history.
Window.getAttention()
非標準 已棄用-
Flashes the application icon.
Window.getAttentionWithCycleCount()
- {{todo("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()
(en-US) 非標準-
Gets default computed style for the specified element, ignoring author stylesheets.
Window.getSelection()
(en-US)-
Returns the selection object representing the selected item(s).
Window.home()
非標準 已棄用-
Returns the browser to the home page.
Window.matchMedia()
(en-US)-
Returns a
MediaQueryList
object representing the specified media query string. Window.maximize()
- {{todo("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()
(en-US)-
Opens a new window.
Window.openDialog()
(en-US) 非標準 已棄用-
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()
-
Opens the Print Dialog to print the current document.
Window.prompt()
(en-US)-
Returns the text entered by the user in a prompt dialog.
Window.releaseEvents()
(en-US) 非標準 已棄用-
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()
實驗性質-
Enables the scheduling of tasks during a browser's idle periods.
Window.resizeBy()
(en-US)-
Resizes the current window by a certain amount.
Window.resizeTo()
(en-US)-
Dynamically resizes window.
Window.restore()
(en-US) 非標準 已棄用- {{todo("NeedsContents")}}
Window.routeEvent()
已棄用- {{todo("NeedsContents")}}
Window.scroll()
(en-US)-
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()
(en-US) 非標準-
Scrolls the document by the given number of lines.
Window.scrollByPages()
(en-US) 非標準-
Scrolls the current document by the specified number of pages.
Window.scrollTo()
(en-US)-
Scrolls to a particular set of coordinates in the document.
Window.setCursor()
非標準 (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()
非標準-
Toggles a user's ability to resize a window.
Window.sizeToContent()
(en-US) 非標準-
Sizes the window according to its content.
Window.stop()
(en-US)-
This method stops window loading.
Window.updateCommands()
(en-US) 非標準-
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.
atob()
(en-US)-
Decodes a string of data which has been encoded using base-64 encoding.
btoa()
-
Creates a base-64 encoded ASCII string from a string of binary data.
clearInterval()
(en-US)-
Cancels the repeated execution set using
setInterval()
. clearTimeout()
(en-US)-
Cancels the delayed execution set using
setTimeout()
(en-US). createImageBitmap()
(en-US)-
Accepts a variety of different image sources, and returns a
Promise
(en-US) which resolves to anImageBitmap
(en-US). Optionally the source is cropped to the rectangle of pixels originating at (sx, sy) with width sw, and height sh. fetch()
(en-US)-
Starts the process of fetching a resource from the network.
EventTarget.removeEventListener
-
Removes an event listener from the window.
setInterval()
-
Schedules a function to execute every time a given number of milliseconds elapses.
setTimeout()
(en-US)-
Schedules a function to execute in a given amount of time.
Obsolete methods
Window.showModalDialog()
(en-US) 已棄用-
Displays a modal dialog. This method was removed completely in Chrome 43, and Firefox 55.
事件處理器
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 WindowEventHandlers
.
備註: 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 (en-US) 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
-
An event handler property for before-unload events on the window.
GlobalEventHandlers.onblur
(en-US)-
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
-
Called after the window is closed
-
Called when the RIGHT mouse button is pressed
Window.ondevicelight
-
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) 非標準 Chrome only-
An event handler property for any device orientation changes.
Window.ondeviceproximity
-
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
(en-US)-
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
(en-US)-
Called when you begin pressing ANY key. See
keydown
(en-US) event. GlobalEventHandlers.onkeypress
(en-US)-
Called when a key (except Shift, Fn, and CapsLock) is in pressed position. See
keypress
(en-US) event. GlobalEventHandlers.onkeyup
(en-US)-
Called when you finish releasing ANY key. See
keyup
event. WindowEventHandlers.onlanguagechange
(en-US)-
An event handler property for
languagechange
(en-US) events on the window. GlobalEventHandlers.onload
(en-US)-
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 event handler representing the code to be called when the
message
event is raised. GlobalEventHandlers.onmousedown
(en-US)-
Called when ANY mouse button is pressed.
GlobalEventHandlers.onmousemove
(en-US)-
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
-
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()
(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
-
An event handler property for paint events on the window.
WindowEventHandlers.onpopstate
-
Called when a back putton is pressed.
Window.onrejectionhandled
(en-US) 實驗性質-
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 event handler 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) 實驗性質-
An event handler for unhandled
Promise
rejection events. WindowEventHandlers.onunload
(en-US)-
Called when the user navigates away from the page.
Window.onuserproximity
-
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).
建構式
See also the DOM Interfaces (en-US).
DOMParser
-
DOMParser
can parse XML or HTML source stored in a string into a DOM Document (en-US).DOMParser
is specified in DOM Parsing and Serialization. Window.GeckoActiveXObject
- {{todo("NeedsContents")}}
Image
(en-US)-
Used for creating an
HTMLImageElement
. Option
(en-US)-
Used for creating an
HTMLOptionElement
(en-US) Window.QueryInterface
- {{todo("NeedsContents")}}
Window.XMLSerializer
- {{todo("NeedsContents")}}
Worker
(en-US)-
Used for creating a Web worker (en-US)
Window.XPCNativeWrapper
- {{todo("NeedsContents")}}
Window.XPCSafeJSObjectWrapper
- {{todo("NeedsContents")}}
相關介面
請參閱 DOM Reference