Browser Window Object
The Window
object, representing a browser window or tab, is the topmost object of a web page and provides various functionalities necessary for interacting with the web page.
The Window object signifies the browser window, and includes several built-in objects, functions, and variables.
The document
object, which represents the web page's document, is also a property of the Window object.
Key Methods of the window object
alert()
: Displays a dialog box with a specified message.
alert method
window.alert('Hello!'); // A dialog box with the message "Hello!" will be displayed.
prompt()
: Displays a dialog box that prompts the user for input.
prompt method
let name = window.prompt('Please enter your name.'); // The user's input will be stored in the name variable.
setTimeout()
: Executes a function after a specified amount of time has elapsed.
setTimeout method
window.setTimeout(() => {
alert('2 seconds have passed!');
}, 2000); // After 2 seconds, a dialog box with the message "2 seconds have passed!" will be displayed.
location
: An object that contains information and methods related to the current window's URL.
location object
console.log(window.location.href); // Outputs the current page's URL.
Important Notes
When using methods or properties of the Window
object, in most cases, the window.
prefix can be omitted. For example, you can simply write alert("Hello!")
instead of window.alert("Hello!")
.
Want to learn more?
Join CodeFriends Plus membership or enroll in a course to start your journey.