Skip to content

Methods of window object in JavaScript

  • by

JavaScript window object methods perform specific tasks like opening, maximizing, minimizing the window, etc.

List of the most commonly used window object methods:

MethodDescription
alert()Displays an alert box with a message and an OK button
atob()Decodes a base-64 encoded string
blur()Removes focus from the current window
btoa()Encodes a string in base-64
clearInterval()Clears a timer set with setInterval()
clearTimeout()Clears a timer set with setTimeout()
close()Closes the current window
confirm()Displays a dialog box with a message and an OK and a Cancel button
focus()Sets focus to the current window
getComputedStyle()Gets the current computed CSS styles applied to an element
getSelection()Returns a Selection object representing the range of text selected by the user
matchMedia()Returns a MediaQueryList object representing the specified CSS media query string
moveBy()Moves a window relative to its current position
moveTo()Moves a window to the specified position
open()Opens a new browser window
print()Prints the content of the current window
prompt()Displays a dialog box that prompts the visitor for input
requestAnimationFrame()Requests the browser to call a function to update an animation before the next repaint
resizeBy()Resizes the window by the specified pixels
resizeTo()Resizes the window to the specified width and height
scroll()Deprecated. This method has been replaced by the scrollTo() method.
scrollBy()Scrolls the document by the specified number of pixels
scrollTo()Scrolls the document to the specified coordinates
setInterval()Calls a function or evaluates an expression at specified intervals (in milliseconds)
setTimeout()Calls a function or evaluates an expression after a specified number of milliseconds
stop()Stops the window from loading

Note : The window is the Global object in a browser and Global object will always be the last place that is searched for something. So, omitting window is OK because it will be found at the end of the “scope chain”.

Methods of window object in JavaScript

Simple example code creates a new window, using the open() method. For example, it provides the URL to be opened in the new window, the name of the window, the width, and the height of the window to be created.

<!DOCTYPE html>
<html>
<body>

  <button onclick="createWindow()">Open a Window</button>
  <p id="result"></p>

  <script>
    function createWindow() {
      let url = "https://tutorial.eyehunts.com/";
      let win = window.open(url, "My New Window", "width=350, height=250");
      document.getElementById("result").innerHTML = win.name + " - " + win.opener.location;
    }
  </script>
</script>
</body>
</html>

Output:

Methods of window object in JavaScript

Here are the methods of the window object in JavaScript listed in a single code snippet:

// Display an alert dialog box
window.alert("Hello, World!");

// Display a confirmation dialog box
var result = window.confirm("Are you sure you want to proceed?");

// Display a dialog box prompting for user input
var name = window.prompt("Please enter your name:", "John Doe");

// Execute a function after a specified delay
setTimeout(function() {
    console.log("Delayed message");
}, 2000);

// Execute a function at specified intervals
setInterval(function() {
    console.log("Repeated message");
}, 1000);

// Clear a timeout specified by setTimeout()
var timeoutID = setTimeout(function() {
    console.log("This message will never be displayed");
}, 2000);
clearTimeout(timeoutID);

// Clear an interval specified by setInterval()
var intervalID = setInterval(function() {
    console.log("This message will be stopped");
}, 1000);
clearInterval(intervalID);

// Open a new browser window or tab
window.open("https://www.example.com");

// Close the current browser window
window.close();

These are various methods of the window object in JavaScript that allows you to interact with the browser environment.

Do comment if you have any doubts or suggestions on this JS window object topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading