J

JavaScript Handbook

Clean • Professional

Window Object in JavaScript

2 minute

Window Object

The window object is the top-level object in the browser’s JavaScript hierarchy. It represents the browser window or tab in which your webpage is displayed.

Every global variable, function, or object automatically becomes a property of the window object.

What is the Window Object?

The window object is part of the Browser Object Model (BOM) and acts as a global container for everything in a web page.

  • When you open a web page, the browser creates a window object for that page.
  • This object contains methods, properties, and other objects (like document, location, navigator, history, and screen).

Example:

console.log(window); // Displays all window properties and methods

Accessing the Window Object

You can access the window object in two ways:

window.alert("Hello!");
alert("Hello!");  // Both are the same

Key Properties of the Window Object

PropertyDescriptionExample
window.innerWidthWidth of the content areaconsole.log(window.innerWidth)
window.innerHeightHeight of the content areaconsole.log(window.innerHeight)
window.outerWidthTotal browser window widthconsole.log(window.outerWidth)
window.outerHeightTotal browser window heightconsole.log(window.outerHeight)
window.screenXX position of window on screenconsole.log(window.screenX)
window.screenYY position of window on screenconsole.log(window.screenY)
window.locationCurrent URL infoconsole.log(window.location.href)
window.documentThe DOM document inside windowconsole.log(window.document.title)
window.navigatorBrowser informationconsole.log(window.navigator.userAgent)
window.historyBrowser navigation historyconsole.log(window.history.length)

Window Object Methods

MethodDescriptionExample
alert()Displays a popup alert boxwindow.alert("Welcome!")
confirm()Shows OK/Cancel dialogwindow.confirm("Are you sure?")
prompt()Gets user input through dialogwindow.prompt("Enter your name:")
open()Opens a new browser window/tabwindow.open("<https://example.com>")
close()Closes the current browser windowwindow.close()
setTimeout()Executes code after a delaysetTimeout(() => alert("Hi"), 2000)
setInterval()Executes repeatedly after intervalsetInterval(() => console.log("Ping"), 1000)
clearTimeout()Cancels a timeoutclearTimeout(timer)
clearInterval()Cancels an intervalclearInterval(intervalId)
scrollTo()Scrolls to specific positionwindow.scrollTo(0, 100)
focus()Focuses on the current windowwindow.focus()
blur()Removes focus from the windowwindow.blur()

Example – Using Common Window Methods

<!DOCTYPE html>
<html>
<body>
  <h2>Window Object Example</h2>
  <button onclick="showAlert()">Show Alert</button>
  <button onclick="askUser()">Ask User</button>
  <button onclick="openSite()">Open Site</button>

  <script>
    function showAlert() {
      alert("Hello! This is an alert message!");
    }

    function askUser() {
      const answer = confirm("Do you like JavaScript?");
      alert(answer ? "Awesome!" : "Let’s learn more!");
    }

    function openSite() {
      window.open("<https://www.google.com>", "_blank", "width=600,height=400");
    }
  </script>
</body>
</html>

Window Timers: setTimeout() & setInterval()

These methods let you execute code after a delay or repeatedly.

Example – setTimeout():

setTimeout(() => {
  alert("This message appears after 3 seconds!");
}, 3000);

Example – setInterval():

let count = 0;
let timer = setInterval(() => {
  console.log("Ping:", ++count);
  if (count === 5) clearInterval(timer); // Stop after 5 times
}, 1000);

Opening and Closing Windows

You can open new browser windows or tabs using window.open().

Example:

let newWin = window.open("<https://example.com>", "_blank", "width=500,height=400");

Close a window:

newWin.close();

 

Article 0 of 0