Skip to content

Methods of window object in JavaScript

  • by

JavaScript window object methods are used to 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

Ntoe : 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 wind up being found at the end of the “scope chain”.

Methods of window object in JavaScript

Simple example code create a new window, using the open() method. In example ew provide the URL to be opened in the new window, 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

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

Your email address will not be published. Required fields are marked *