Window Object
In this tutorial, we will learn about the Window object in JavaScript.
In JavaScript, the window object represents the current browser window or tab that is open in a web browser. It is a global object that provides access to various properties and methods related to the browser window. The window object is automatically created by the browser and is available to all JavaScript code running in the browser.
Accessing the Window Object
You can access the window object in JavaScript using the window keyword. For example, you can use the window object to get the current URL of the browser window:
let currentURL = window.location.href;
console.log(currentURL);This will output the current URL of the browser window.
Properties of the Window Object
The window object has many properties that provide information about the browser window. Some of the commonly used properties are:
window.location: Provides information about the current URL of the browser window.window.document: Represents the document object of the current web page.window.navigator: Provides information about the browser and the operating system.window.screen: Provides information about the screen size and resolution.window.history: Provides access to the browser's history stack.
You can access these properties using the window object. For example, to get the current URL of the browser window, you can use window.location.href.
Methods of the Window Object
The window object also has many methods that allow you to interact with the browser window. Some of the commonly used methods are:
window.alert(): Displays an alert dialog box with a message and an OK button.window.prompt(): Displays a dialog box that prompts the user for input.window.confirm(): Displays a dialog box with a message and OK/Cancel buttons.
You can call these methods using the window object. For example, to display an alert dialog box, you can use window.alert('Hello, World!').
Conclusion
- The
windowobject represents the current browser window or tab in JavaScript. - You can access the
windowobject using thewindowkeyword. - The
windowobject has many properties and methods that allow you to interact with the browser window.
How is this guide?
Sign in to share your feedback
Help us improve by sharing your thoughts on this guide.
Last updated on