Window Object Methods
A method in JavaScript is a function connected to an object. You may conduct operations or compute values by calling methods on objects.
Syntax:
window.MethodName()
NOTE: Depending on the attributes, a parameter could have any value, including a string, number, object, and more.
Here is a list of some of the methods of the window object:
Property Name | Description |
window.open() | This method opens a new browser window or tab. |
window.close() | This method closes the current window or tab. |
window.alert() | This method displays an alert message to the user. |
window.prompt() | This method displays a prompt message to the user and waits for their input. |
window.confirm() | This method displays a confirm message to the user and waits for their response.window.focus: brings the current window or tab to the front. |
window.blur() | Sends the current window or tab to the back. |
window.postMessage() | Sends a message to the window or frame that is targeted by the specified WindowProxy object. |
window.scrollTo() | Scrolls the window or frame to a specific position. |
window.scrollBy() | Scrolls the window or frame by a specific number of pixels. |
window.resizeTo() | Resizes the window to a specific size. |
window.resizeBy() | Resizes the window by a specific number of pixels. |
window.atob() | A base-64 encoded string is decoded via atob(). |
window.btoa() | Base-64 encoding is done with btoa(). |
window.clearInterval() | A timer set with setInterval() is reset. |
window.clearTimeout() | The function clearTimeout() resets a timer specified with setTimeout(). |
window.focus() | It switches the focus to the active window. |
window.getComputedStyle() | This function returns the element’s current computed CSS styles. |
window.getSelection() | It provides a Selection object corresponding to the user-selected text selection range. |
window.matchMedia() | The provided CSS media query string is represented by a MediaQueryList object created by the matchMedia() function. |
window.moveBy() | Relocates a window with respect to its present location. |
window.moveTo() | Relocates a window to the given location. |
window.print() | Displays what is currently displayed in the window. |
window.requestAnimationFrame() | Before the subsequent repaint, the browser is asked to invoke a function to update an animation using the requestAnimationFrame() method. |
window.setInterval() | At predetermined intervals, setInterval() calls a function or evaluates an expression (in milliseconds). |
window.setTimeout() | When a certain amount of milliseconds have passed, the setTimeout() method calls a function or evaluates an expression. |
window.stop() | It halts the loading of the window. |
Window Object Method Example
Example 1: We’ll use the window.alert method in this example to show how it works:
// This is window.alert method
console.log(window.alert("w3wiki"))
Output:
Example 2: In this example, we’ll use the setInterval() Method:
<!DOCTYPE html>
<html>
<body>
<h2>The setInterval() Method</h2>
<p id="demo"></p>
<script>
const element = document.getElementById("demo");
setInterval(function () {
element.innerHTML += "w3wiki</br>"
}, 500);
</script>
</body>
</html>
Output:
Window Object in JavaScript
In JavaScript, the Window
object represents the window that contains a DOM document.
The Window
object provides access to various properties and methods that enable interaction with the browser environment, including manipulating the document, handling events, managing timers, displaying dialog boxes, and more.
Contact Us