-->

JavaScript - Window - The Browser Object Model Part 2: Window Methods

The window object includes various methods for interacting with the browser.

Examples and Explanation

Alert, Confirm, and Prompt

window.alert("This is an alert!");

const result = window.confirm("Do you agree?");

const name = window.prompt("What is your name?");

console.log({ result, name });

Explanation: These methods create dialog boxes for user interaction.

Opening a New Window

const newWindow = window.open("https://www.example.com", "_blank", "width=400,height=400");

Explanation: The open() method opens a new browser window or tab.

Closing a Window

newWindow.close();

Explanation: The close() method is used to close a window opened using open().