How to Use the JavaScript Window Object?

Estimated read time 2 min read

The window object in JavaScript is the top-level object that represents the current browser window or tab. It provides access to many properties and methods that are related to the window, including the size, position, and state of the window, as well as methods for interacting with the document, opening and closing windows, and managing the history of the browser.

Here are some examples of how to use the window object:

  1. Get the size of the window:
var windowWidth = window.innerWidth;
var windowHeight = window.innerHeight;
console.log("Window size: " + windowWidth + " x " + windowHeight);

In this example, the innerWidth and innerHeight properties of the window object are used to get the width and height of the window, respectively.

  1. Open a new window:
window.open("https://www.example.com", "ExampleWindow", "width=400,height=300");

In this example, the open method of the window object is used to open a new window with a specified URL, name, and size.

  1. Change the location of the current window:
window.location.href = "https://www.example.com";

In this example, the href property of the location object is used to change the location of the current window to a new URL.

These are just a few examples of how to use the window object. There are many other properties and methods available on the window object, and you can find more information about them in the JavaScript documentation.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply