window
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 onewindow
object for the page containing the<iframe>
tags, and an additionalwindow
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.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn JavaScript on Codecademy
- Career path
Front-End Engineer
Front-end engineers work closely with designers to make websites beautiful, functional, and fast.Includes 34 CoursesWith Professional CertificationBeginner Friendly115 hours - Free course
Learn JavaScript
Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.Beginner Friendly15 hours