Window Object
JavaScript BOM (Browser Object Model) - Window Object Tutorial
Table of Contents
Introduction to JavaScript BOM
What is the Window Object?
Properties of the Window Object
window.innerWidth
andwindow.innerHeight
window.outerWidth
andwindow.outerHeight
window.location
window.history
window.navigator
window.screen
window.document
Methods of the Window Object
window.alert()
window.confirm()
window.prompt()
window.open()
window.close()
window.setTimeout()
window.setInterval()
window.clearTimeout()
window.clearInterval()
Working with the Window Location Object
window.location.href
window.location.reload()
window.location.assign()
window.location.replace()
Working with the Window History Object
window.history.back()
window.history.forward()
window.history.go()
Working with the Window Navigator Object
window.navigator.userAgent
window.navigator.platform
window.navigator.language
Window Events
window.onload
window.onresize
window.onscroll
window.onbeforeunload
Conclusion
1. Introduction to JavaScript BOM
Welcome to the Codes with Pankaj tutorial on JavaScript BOM (Browser Object Model)! In this tutorial, we'll explore the window
object, which is a part of the BOM. The Browser Object Model allows JavaScript to interact with the browser and control aspects of the web page that are beyond the document itself. Let's dive in!
The BOM includes objects like window
, location
, history
, navigator
, and screen
. In this tutorial, we'll focus on the window
object and its properties and methods.
2. What is the Window Object?
The window
object is the global object in the browser environment. It represents the browser window or tab and provides methods and properties to interact with the browser itself.
In JavaScript, all global variables and functions are automatically properties of the window
object.
Example:
3. Properties of the Window Object
window.innerWidth
and window.innerHeight
These properties return the width and height of the content area of the browser window (excluding toolbars and scrollbars).
Example:
window.outerWidth
and window.outerHeight
These properties return the width and height of the browser window, including toolbars, scrollbars, and borders.
Example:
window.location
The location
property returns the Location
object, which contains information about the current URL of the window.
Example:
window.history
The history
property returns the History
object, which allows you to navigate back and forth through the browser's history.
Example:
window.navigator
The navigator
property returns the Navigator
object, which provides information about the browser and operating system.
Example:
window.screen
The screen
property returns the Screen
object, which contains information about the user's screen.
Example:
window.document
The document
property returns the Document
object, which represents the content of the web page.
Example:
4. Methods of the Window Object
window.alert()
Displays an alert dialog with a specified message and an OK button.
Example:
window.confirm()
Displays a dialog with a specified message and OK and Cancel buttons. Returns true
if OK is clicked, and false
if Cancel is clicked.
Example:
window.prompt()
Displays a dialog with a text input field, an OK button, and a Cancel button. Returns the text entered by the user or null
if Cancel is clicked.
Example:
window.open()
Opens a new browser window or tab with a specified URL.
Example:
window.close()
Closes the current browser window. This method only works if the window was opened using window.open()
.
Example:
window.setTimeout()
Executes a function after a specified delay (in milliseconds).
Example:
window.setInterval()
Executes a function repeatedly at specified intervals (in milliseconds).
Example:
window.clearTimeout()
Cancels a timeout set with window.setTimeout()
.
Example:
window.clearInterval()
Cancels an interval set with window.setInterval()
.
Example:
5. Working with the Window Location Object
The window.location
object contains information about the current URL and provides methods to redirect the user or reload the page.
window.location.href
Returns the full URL of the current page.
Example:
window.location.reload()
Reloads the current page.
Example:
window.location.assign()
Loads a new document.
Example:
window.location.replace()
Replaces the current document with a new one (no back navigation to the replaced document).
Example:
6. Working with the Window History Object
The window.history
object allows you to navigate through the browser's history.
window.history.back()
Navigates to the previous page.
Example:
window.history.forward()
Navigates to the next page in the history.
Example:
window.history.go()
Navigates to a specific page in the history. Positive values move forward, and negative values move back.
Example:
7. Working with the Window Navigator Object
The window.navigator
object provides information about the browser and the operating system.
window.navigator.userAgent
Returns the user agent string representing the browser.
Example:
window.navigator.platform
Returns the platform on which the browser is running.
Example:
window.navigator.language
Returns the language of the browser.
Example:
8. Window Events
The window
object can handle several events to perform actions when the browser window is resized, loaded, scrolled, or about to be closed.
window.onload
Executes a function when the window has finished loading.
Example:
window.onresize
Executes a function when the window is resized.
Example:
window.onscroll
Executes a function when the user scrolls the window.
Example:
window.onbeforeunload
Displays a message when the user attempts to leave the page.
Example:
9. Conclusion
In this detailed tutorial, we explored the window
object, a core component of the Browser Object Model (BOM) in JavaScript. We covered its properties, methods, and how to interact with other BOM objects like location
, history
, and navigator
. The window
object is essential for controlling the browser and handling user interactions in web applications.
For more tutorials and examples, visit www.codeswithpankaj.com! Happy coding!
Last updated