Window

翻譯不完整。請協助 翻譯此英文文件

window 物件代表了一個包含 DOM 文件的視窗,其中的 document 屬性指向了視窗中載入的 Document 物件。而使用 document defaultView 屬性,則可取得該 Document 物件所在的視窗 window 物件。

本節提供了 DOM window 物件的所有方法、屬性以及事件的說明。window 物件實作了 Window 介面,而 Window 介面則繼承自 AbstractView 介面。一些額外、與 window 物件沒有直接關係,但卻置於 window 物件中的全域函式、命名空間、物件、介面以及建構式,將會在 JavaScript Reference 和 DOM Reference 中說明。

在支援分頁的瀏覽器中(如 Firefox),每一個分頁標籤都擁有自己的 window 物件(如果你正在撰寫瀏覽器附加元件,瀏覽器視窗本身也是一個個別的 window-請參閱 Working with windows in chrome code),分頁不會於同一個瀏覽器視窗中共享 window 物件。但有部分的方法,如 window.resizeTo() 或 window.resizeBy() 會套用至整個視窗,而不只是該 window 物件所屬的分頁標籤。一般來說,若無法適當的應用於分頁,便會套用至瀏覽器視窗。

屬性

請注意,指向物件的屬性(例如覆蓋了內建物件原型的元素)會在另外的列表中說明。

Window.applicationCache  Read only
指向 OfflineResourceList 物件,此物件提供了視窗的離線資源。
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 providing 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 已過時 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 an nsIArray.
Window.directories
Synonym of window.personalbar
Window.document Read only
指向此 window 中的 document 物件。
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
此屬性表示目前視窗是否為全螢幕顯示。
Window.globalStorage 已過時 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
指向 history 物件。
Window.innerHeight
取得視窗內的網頁內容高度,包含水平捲軸。
Window.innerWidth
取得視窗內的網頁內容寬度,包含垂直捲軸。
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 in nsIDOMWindowUtils 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
取得瀏覽器視窗的高度。
Window.outerWidth Read only
取得瀏覽器視窗的寬度。
Window.pageXOffset Read only
An alias for window.scrollX.
Window.pageYOffsetRead 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 已過時 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. See Window.frames for more details.

方法

Window.alert()
Displays an alert dialog.
Window.back()
Moves back one in the window history.
Window.blur()
Sets focus away from the window.
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.
Window.close()
Closes the current window.
Window.confirm()
Displays a dialog with a message that the user needs to respond to.
Window.disableExternalCapture() 已過時 Gecko 24
FIXME: NeedsContents
Window.dump()
Writes a message to the console.
Window.enableExternalCapture() 已過時 Gecko 24
FIXME: NeedsContents
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.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() 已過時 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
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).

此介面繼承了其父介面 EventTarget 的方法,並且實作了 WindowTimersWindowBase64 及 GlobalFetch 的方法:

EventTarget.addEventListener()
EventTarget 物件上註冊指定事件的監聽器。
EventTarget.removeEventListener()
移除 EventTarget 物件上的指定事件監聽器。
EventTarget.dispatchEvent()
對此 EventTarget 物件派送(dispatch)一個事件物件,也就是於此 EventTarget 物件上觸發一個指定的事件物件實體。

Mozilla chrome code 的額外方法

Mozilla extensions for use by JS-implemented event targets to implement on* properties. See also WebIDL bindings.

  • void setEventHandler(DOMString type, EventHandler handler)
  • EventHandler getEventHandler(DOMString type)

This interface does not inherit any method.

WindowTimers.clearInterval()
Cancels the repeated execution set using WindowTimers.setInterval().
WindowTimers.clearTimeout()
Cancels the delayed execution set using WindowTimers.setTimeout().
WindowTimers.setInterval()
Schedules the execution of a function each X milliseconds.
WindowTimers.setTimeout()
Sets a delay for executing a function.

This helper does not inherit any methods.

WindowBase64.atob()
Decodes a string of data which has been encoded using base-64 encoding.
WindowBase64.btoa()
Creates a base-64 encoded ASCII string from a string of binary data.

GlobalFetch.fetch()
Starts the process of fetching a resource.

事件處理器

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.

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.

Window.onbeforeinstallprompt
An event handler property dispatched before a user is prompted to save a web site to a home screen on mobile.
Window.ondevicelight
An event handler property for any ambient light levels changes
Window.ondevicemotion
FIXME: NeedsContents
Window.ondeviceorientation
An event handler property for any relative device orientation changes.
Window.ondeviceorientationabsolute  Chrome only
An event handler property for any device orientation changes.
Window.ondeviceproximity
An event handler property for device proximity event
Window.oninstall
An event handler property that is called when the install event is send on the window, that is when the page is installed as an application.
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 the Window.mozRequestAnimationFrame() method.
Window.onpaint
An event handler property for paint events on the window.
Window.onrejectionhandled
An event handler for handled Promise rejection events.
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).

此介面繼承了 GlobalEventHandlers 的事件處理器,並且實作了 WindowEventHandlers 的事件處理器:

The events properties, of the form onXYZ, are defined on the GlobalEventHandlers, and implemented by HTMLElement, Document, Window, and WorkerGlobalScope for Web Workers.

GlobalEventHandlers.onabort
Is an EventHandler representing the code to be called when the abort event is raised.
GlobalEventHandlers.onblur
Is an EventHandler representing the code to be called when the blur event is raised.
GlobalEventHandlers.onerror
Is an OnErrorEventHandler representing the code to be called when the error event is raised.
GlobalEventHandlers.onfocus
Is an EventHandler representing the code to be called when the focus event is raised.
GlobalEventHandlers.oncancel
Is an EventHandler representing the code to be called when the cancel event is raised.
GlobalEventHandlers.oncanplay
Is an EventHandler representing the code to be called when the canplay event is raised.
GlobalEventHandlers.oncanplaythrough
Is an EventHandler representing the code to be called when the canplaythrough event is raised.
GlobalEventHandlers.onchange
Is an EventHandler representing the code to be called when the change event is raised.
GlobalEventHandlers.onclick
Is an EventHandler representing the code to be called when the click event is raised.
GlobalEventHandlers.onclose
Is an EventHandler representing the code to be called when the close event is raised.
GlobalEventHandlers.oncontextmenu
Is an EventHandler representing the code to be called when the contextmenu event is raised.
GlobalEventHandlers.oncuechange
Is an EventHandler representing the code to be called when the cuechange event is raised.
GlobalEventHandlers.ondblclick
Is an EventHandler representing the code to be called when the dblclick event is raised.
GlobalEventHandlers.ondrag
Is an EventHandler representing the code to be called when the drag event is raised.
GlobalEventHandlers.ondragend
Is an EventHandler representing the code to be called when the dragend event is raised.
GlobalEventHandlers.ondragenter
Is an EventHandler representing the code to be called when the dragenter event is raised.
GlobalEventHandlers.ondragexit
Is an EventHandler representing the code to be called when the dragexit event is raised.
GlobalEventHandlers.ondragleave
Is an EventHandler representing the code to be called when the dragleave event is raised.
GlobalEventHandlers.ondragover
Is an EventHandler representing the code to be called when the dragover event is raised.
GlobalEventHandlers.ondragstart
Is an EventHandler representing the code to be called when the dragstart event is raised.
GlobalEventHandlers.ondrop
Is an EventHandler representing the code to be called when the drop event is raised.
GlobalEventHandlers.ondurationchange
Is an EventHandler representing the code to be called when the durationchange event is raised.
GlobalEventHandlers.onemptied
Is an EventHandler representing the code to be called when the emptied event is raised.
GlobalEventHandlers.onended
Is an EventHandler representing the code to be called when the ended event is raised.
GlobalEventHandlers.oninput
Is an EventHandler representing the code to be called when the input event is raised.
GlobalEventHandlers.oninvalid
Is an EventHandler representing the code to be called when the invalid event is raised.
GlobalEventHandlers.onkeydown
Is an EventHandler representing the code to be called when the keydown event is raised.
GlobalEventHandlers.onkeypress
Is an EventHandler representing the code to be called when the keypress event is raised.
GlobalEventHandlers.onkeyup
Is an EventHandler representing the code to be called when the keyup event is raised.
GlobalEventHandlers.onload
Is an EventHandler representing the code to be called when the load event is raised.
GlobalEventHandlers.onloadeddata
Is an EventHandler representing the code to be called when the loadeddata event is raised.
GlobalEventHandlers.onloadedmetadata
Is an EventHandler representing the code to be called when the loadedmetadata event is raised.
GlobalEventHandlers.onloadstart
Is an EventHandler representing the code to be called when the loadstart event is raised.
GlobalEventHandlers.onmousedown
Is an EventHandler representing the code to be called when the mousedown event is raised.
GlobalEventHandlers.onmouseenter
Is an EventHandler representing the code to be called when the mouseenter event is raised.
GlobalEventHandlers.onmouseleave
Is an EventHandler representing the code to be called when the mouseleave event is raised.
GlobalEventHandlers.onmousemove
Is an EventHandler representing the code to be called when the mousemove event is raised.
GlobalEventHandlers.onmouseout
Is an EventHandler representing the code to be called when the mouseout event is raised.
GlobalEventHandlers.onmouseover
Is an EventHandler representing the code to be called when the mouseover event is raised.
GlobalEventHandlers.onmouseup
Is an EventHandler representing the code to be called when the mouseup event is raised.
GlobalEventHandlers.onmousewheel
Is an EventHandler representing the code to be called when the mousewheel event is raised.
GlobalEventHandlers.onpause
Is an EventHandler representing the code to be called when the pause event is raised.
GlobalEventHandlers.onplay
Is an EventHandler representing the code to be called when the play event is raised.
GlobalEventHandlers.onplaying
Is an EventHandler representing the code to be called when the playing event is raised.
GlobalEventHandlers.onpointerdown
Is an EventHandler representing the code to be called when the pointerdown event is raised.
GlobalEventHandlers.onpointermove
Is an EventHandler representing the code to be called when the pointermove event is raised.
GlobalEventHandlers.onpointerup
Is an EventHandler representing the code to be called when the pointerup event is raised.
GlobalEventHandlers.onpointercancel
Is an EventHandler representing the code to be called when the pointercancel event is raised.
GlobalEventHandlers.onpointerover
Is an EventHandler representing the code to be called when the pointerover event is raised.
GlobalEventHandlers.onpointerout
Is an EventHandler representing the code to be called when the pointerout event is raised.
GlobalEventHandlers.onpointerenter
Is an EventHandler representing the code to be called when the pointerevent event is raised.
GlobalEventHandlers.onpointerleave
Is an EventHandler representing the code to be called when the pointerleave event is raised.
GlobalEventHandlers.onpointerlockchange
Is an EventHandler representing the code to be called when the pointerlockchange event is raised.
GlobalEventHandlers.onpointerlockerror
Is an EventHandler representing the code to be called when the pointerlockerror event is raised.
GlobalEventHandlers.onprogress
Is an EventHandler representing the code to be called when the progress event is raised.
GlobalEventHandlers.onratechange
Is an EventHandler representing the code to be called when the ratechange event is raised.
GlobalEventHandlers.onreset
Is an EventHandler representing the code to be called when the reset event is raised.
GlobalEventHandlers.onscroll
Is an EventHandler representing the code to be called when the scroll event is raised.
GlobalEventHandlers.onseeked
Is an EventHandler representing the code to be called when the seeked event is raised.
GlobalEventHandlers.onseeking
Is an EventHandler representing the code to be called when the seeking event is raised.
GlobalEventHandlers.onselect
Is an EventHandler representing the code to be called when the select event is raised.
GlobalEventHandlers.onselectionchange
Is an EventHandler representing the code to be called when the selectionchange event is raised.
GlobalEventHandlers.onshow
Is an EventHandler representing the code to be called when the show event is raised.
GlobalEventHandlers.onsort
Is an EventHandler representing the code to be called when the sort event is raised.
GlobalEventHandlers.onstalled
Is an EventHandler representing the code to be called when the stalled event is raised.
GlobalEventHandlers.onsubmit
Is an EventHandler representing the code to be called when the submit event is raised.
GlobalEventHandlers.onsuspend
Is an EventHandler representing the code to be called when the suspend event is raised.
GlobalEventHandlers.ontimeupdate
Is an EventHandler representing the code to be called when the timeupdate event is raised.
GlobalEventHandlers.onvolumechange
Is an EventHandler representing the code to be called when the volumechange event is raised.
GlobalEventHandlers.ontouchcancel
Is an EventHandler representing the code to be called when the touchcancel event is raised.
GlobalEventHandlers.ontouchend
Is an EventHandler representing the code to be called when the touchend event is raised.
GlobalEventHandlers.ontouchmove
Is an EventHandler representing the code to be called when the touchmove event is raised.
GlobalEventHandlers.ontouchstart
Is an EventHandler representing the code to be called when the touchstart event is raised.
GlobalEventHandlers.onwaiting
Is an EventHandler representing the code to be called when the waiting event is raised.

The events properties, of the form onXYZ, are defined on the WindowEventHandlers, and implemented by Window, and WorkerGlobalScope for Web Workers.

WindowEventHandlers.onafterprint
Is an EventHandler representing the code to be called when the afterprint event is raised.
WindowEventHandlers.onbeforeprint
Is an EventHandler representing the code to be called when the beforeprint event is raised.
WindowEventHandlers.onbeforeunload
Is an EventHandler representing the code to be called when the beforeunload event is raised.
WindowEventHandlers.onhashchange
Is an EventHandler representing the code to be called when the hashchange event is raised.
WindowEventHandlers.onlanguagechange
Is an EventHandler representing the code to be called when the languagechange event is raised.
WindowEventHandlers.onmessage
Is an EventHandler representing the code to be called when the message event is raised.
WindowEventHandlers.onoffline
Is an EventHandler representing the code to be called when the offline event is raised.
WindowEventHandlers.ononline
Is an EventHandler representing the code to be called when the online event is raised.
WindowEventHandlers.onpagehide
Is an EventHandler representing the code to be called when the pagehide event is raised.
WindowEventHandlers.onpageshow
Is an EventHandler representing the code to be called when the pageshow event is raised.
WindowEventHandlers.onpopstate
Is an EventHandler representing the code to be called when the popstate event is raised.
WindowEventHandlers.onstorage
Is an EventHandler representing the code to be called when the storage event is raised.
WindowEventHandlers.onunhandledrejection
An event handler for unhandled Promise rejection events.
WindowEventHandlers.onunload
Is an EventHandler representing the code to be called when the unload event is raised.

建構式

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

相關介面

請參閱 DOM Reference

參見

文件標籤與貢獻者

 此頁面的貢獻者: jackblackevo
 最近更新: jackblackevo,