What is JavaScript BOM


The Browser Object Model (BOM) is a set of objects provided by web browsers that allows JavaScript to interact with the browser's environment. Unlike the Document Object Model (DOM), which is concerned with manipulating HTML content and structure, the BOM deals with the browser window and its associated components. This includes operations related to the browser window, navigation, and interaction with the browser environment.

Key Components of the BOM

  1. window Object

    • The window object is the global object in the browser environment. It represents the browser window and provides access to various properties and methods to interact with it.

    Example:

    // Accessing the window object console.log(window.innerWidth); // Width of the browser viewport console.log(window.location.href); // Current URL // Using window methods window.alert('Hello, World!'); // Display an alert dialog
  2. navigator Object

    • The navigator object provides information about the user's browser and operating system. It includes properties for detecting browser features and capabilities.

    Example:

    // Accessing browser information console.log(navigator.userAgent); // Returns the user-agent string of the browser console.log(navigator.language); // Returns the browser's language setting
  3. location Object

    • The location object provides information about the current URL and allows you to navigate to different URLs. It can be used to get or set parts of the URL or redirect the browser to a new page.

    Example:

    // Getting the current URL console.log(location.href); // Navigating to a new URL location.href = 'https://www.example.com'; // Reloading the current page location.reload();
  4. history Object

    • The history object allows you to interact with the browser's session history. You can navigate back and forward through the user's browsing history.

    Example:

    // Navigating back in history history.back(); // Navigating forward in history history.forward(); // Moving to a specific entry in history history.go(-1); // Go back one page
  5. screen Object

    • The screen object provides information about the user's screen, such as its dimensions and color depth.

    Example:

    // Accessing screen information console.log(screen.width); // Screen width in pixels console.log(screen.height); // Screen height in pixels
  6. localStorage and sessionStorage

    • These objects provide mechanisms for storing data in the browser. localStorage persists data across sessions, while sessionStorage only persists data for the duration of the page session.

    Example:

    // Using localStorage localStorage.setItem('name', 'Alice'); console.log(localStorage.getItem('name')); // Output: Alice // Using sessionStorage sessionStorage.setItem('sessionData', 'some data'); console.log(sessionStorage.getItem('sessionData')); // Output: some data

Practical Examples

Here are a few practical examples demonstrating how to use BOM components:

  1. Displaying an Alert

    window.alert('This is an alert message!');
  2. Redirecting to a New URL

    window.location.href = 'https://www.example.com';
  3. Getting Browser Information

    console.log(`User Agent: ${navigator.userAgent}`);
  4. Handling Browser History

    document.querySelector('#backButton').addEventListener('click', function() { history.back(); }); document.querySelector('#forwardButton').addEventListener('click', function() { history.forward(); });
  5. Storing and Retrieving Data

    // Storing data localStorage.setItem('user', 'John Doe'); // Retrieving data console.log(localStorage.getItem('user'));