window

StevenSwiniarski's avatar
Published May 29, 2023
Contribute to Docs

The window object is the global object of JavaScript when run within a browser and represents the browser window. All browsers support it. It contains as members all global JavaScript objects, functions, and variables. Such variables become properties of the window object, such functions become methods of the window object.

Note: If a browser window contains <iframe> tags, there will be one window object for the page containing the <iframe> tags, and an additional window object for the page in each <iframe>.

Syntax

Below shows the use of a window method, .alert(), to display a message to the user:

window.alert('Hello World!');

Because the window object is global, the “window.” portion can also be omitted:

alert('Hello World!');

The window object has a number of properties and methods, some of which are listed below:

window

.resizeTo()
Resizes the browser window to the width and height specified in pixels.
.scrollTo()
Scrolls document to specified coordinate in pixels.
clearInterval()
Cancels a timer previously established by setInterval().
clearTimeout()
Clears a previously scheduled timeout function.
confirm()
Instructs the browser window to show a optional message, the user then must confirm or cancel the message
prompt()
Displays a dialog with an optional message asking users to input some text.
reportError()
Allows users to report errors to the console or global error handlers.
requestAnimationFrame()
Schedules a function to execute before the next repaint.
resizeBy()
Resizes the browser window by a specified amount.
scroll()
Scrolls the document to a specific position based on given coordinates.
scrollBy()
Scrolls the window content by a specified number of pixels, relative to the current scroll position.
setinterval()
Executes a function repeatedly at specified intervals.
setTimeout()
Allows users to execute callbacks after a period of time expressed in milliseconds.
stop()
Stops the loading of the current window, equivalent to clicking the stop button of browser.
structuredClone()
Creates a deep clone of a JavaScript value, preserving circular references and handling complex objects.

All contributors

Contribute to Docs

Learn JavaScript on Codecademy