Window Object

Window Object

The window object represents the window opened in the browser.

If the document contains frames (iframe tags), the browser will create a window object for the HTML document and additional window objects for each frame.

Window Object Properties

Object Description
closed Returns whether the window has been closed.
console Return the window's Console Object.
defaultStatus deprecated.Sets or returns the default text in the window status bar.
document Return the window's Document Object.
frameElement Returns the frames running in the window.
frames Returns all the window objects running in the window.
history Return the window's History Object.
innerHeight Returns the height of the document display area of the window.
innerWidth Returns the width of the document display area of the window.
length Returns the number of <iframe> elements in the current window.
localStorage Allows key/value pairs to be saved in a Web browser. Stores data without an expiration date.
location Return the window's Location Object.
name Sets or returns the name of the window.
navigator Return the window's Navigator Object.
opener Returns a reference to the window that created the window.
outerHeight Returns the height of the browser window, including the toolbar/scrollbar.
outerWidth Returns the width of the browser window, including the toolbar/scrollbar.
pageXOffset Returns the number of pixels the current document has been scrolled horizontally from the top-left corner of the window.
pageYOffset Returns the number of pixels the current document has been scrolled vertically from the top-left corner of the window.
parent Return the parent window of the current window.
screen Return the window's Screen object.
screenLeft Return the horizontal coordinate of the window relative to the screen.
screenTop Return the vertical coordinate of the window relative to the screen.
screenX Return the horizontal coordinate of the window relative to the screen.
screenY Return the vertical coordinate of the window relative to the screen.
sessionStorage Allow key/value pairs to be saved in a web browser. Store session data.
scrollX pageXOffset Alias.
scrollY pageYOffset Alias.
self Return the current window.
status Deprecated. Avoid using it.
top Return the top-level browser window.

Window object methods

Method Description
alert() Display an alert box (dialog) with a message and an OK button.
atob() Decode a base-64 encoded string.
blur() Remove the focus from the current window.
btoa() Encode a string in base-64.
clearInterval() Clear the timer set using setInterval().
clearTimeout() Clear the timer set using setTimeout().
close() Close the current window.
confirm() Display a dialog box containing a message and OK and Cancel buttons.
focus() Set the focus to the current window.
getComputedStyle() Determine the CSS style applied to a document element.
getSelection() Return a Selection object representing the text range selected by the user.
matchMedia() Return a MediaQueryList object representing the specified CSS media query string.
moveBy() Move the window relative to its current position.
moveTo() Move the window to the specified position.
open() Open a new browser window.
print() Print the content of the current window.
prompt() Prompt for input of a simple string using a dialog box.
requestAnimationFrame() Request the browser to call the function to update the animation before the next repaint.
resizeBy() Adjust the size of the window by the specified pixels.
resizeTo() Adjust the size of the window to the specified width and height.
scroll() deprecated.This method has been scrollTo() method Replace.
scrollBy() Scroll the document by the specified number of pixels.
scrollTo() Scroll the document to the specified coordinates.
setInterval() Execute the specified code periodically.
setTimeout() Execute code after a specified time.
stop() Stop loading the window.

Window object description

The Window object represents the browser window or frame. In client-side JavaScript, the Window object is a global object, and all expressions are evaluated in the current environment. That is, there is no need for special syntax to refer to the current window; you can use the properties of that window as global variables. For example, you can just write document, without writing window.document.

Similarly, you can use the methods of the current window object as functions, such as just writing alert() without writing Window.alert().

In addition to the properties and methods listed above, the Window object also implements all the global properties and methods defined by core JavaScript.

The window property of the Window object and self propertyThey all refer to themselves. When you want to explicitly refer to the current window rather than implicitly refer to it, you can use these two properties. In addition to these two properties, the parent property, the top property, and the frame[] array all refer to other Window objects related to the current Window object.

To refer to a frame within a window, you can use the following syntax:

frame[i]		//The frame of the current window
self.frame[i]	//The frame of the current window
w.frame[i]	//The frame of window w

To refer to the parent window (or parent frame) of a frame, you can use the following syntax:

parent		//The parent window of the current window
self.parent	//The parent window of the current window
w.parent 		//The parent window of window w

To refer to any frame contained within the top-level window, you can use the following syntax:

top		//The top-level window of the current frame
self.top		//The top-level window of the current frame
f.top		//The top-level window of frame f

A new top-level browser window is created by the Window.open() method. When calling this method, the return value of the open() call should be stored in a variable, and then that variable is used to refer to the new window. The new window's opener propertyit refers back to the window that opened it.

Generally, the methods of the Window object are operations on the browser window or frame. And alert() method,confirm() methodand prompt methodare different, they interact with users through simple dialog boxes.