📚Understanding BOM in JavaScript: Fast Explaining and Providing Examples

Maksym Smirnov
Stackademic
Published in
4 min readDec 1, 2023

--

🕹️JavaScript is a versatile programming language that empowers developers to create dynamic and interactive web applications. While its core functionality revolves around manipulating web content through the Document Object Model (DOM), JavaScript also provides the Browser Object Model (BOM) to interact with the browser environment.

💡What is BOM?

BOM stands for Browser Object Model. It represents various objects provided by the browser environment, enabling JavaScript to interact with the browser itself, handle window properties, and manage interactions with the user.

💻Key Components of BOM

1. Window Object

The window object is the root object of the BOM. It represents the browser window and serves as a global object for JavaScript in the web browser environment. It provides access to various properties and methods.

➡️ Example:

// Accessing window properties
console.log(window.innerWidth); // Returns the inner width of the browser window
console.log(window.location.href); // Returns the URL of the current page

2. Document Object

Although part of the DOM, the document object also falls under BOM. It represents the web page itself and provides methods to manipulate its content.

➡️ Example:

// Accessing document properties
console.log(document.title); // Returns the title of the current document
document.getElementById('myElement').style.color = 'red'; // Modifying element style

3. Navigator Object

The navigator object provides information about the browser such as its name, version, and platform.

➡️ Example:

// Accessing navigator properties
console.log(navigator.userAgent); // Returns information about the user agent
console.log(navigator.platform); // Returns the platform (OS) on which the browser is running

4. History Object

The history object allows manipulation of the browser's session history, enabling navigation control within the browsing session.

➡️ Example:

// Using history object
history.back(); // Navigates to the previous page in history
history.forward(); // Navigates to the next page in history

5. Screen Object

The screen object provides information about the user's screen, such as its width, height, and pixel depth.

➡️ Example:

// Accessing screen properties
console.log(screen.width); // Returns the width of the screen
console.log(screen.height); // Returns the height of the screen

📲Practical Use Cases

1. Handling Browser Resize Events

When creating responsive web applications, it’s essential to adapt to various screen sizes. Utilizing the resize event with window.addEventListener enables developers to respond dynamically to changes in the browser window size.

window.addEventListener('resize', function() {
console.log('Window resized');
// Additional actions on window resize
});

In this example, whenever the user resizes the browser window, the function within the event listener will execute. This functionality can be used to adjust the layout, update elements, or trigger specific actions to enhance user experience based on different screen sizes.

2. Opening a New Browser Window

The window.open() method allows developers to open new browser windows or tabs programmatically. This method accepts parameters such as the URL to open and the target window's behavior (like opening in a new tab or window).

let newWindow = window.open('https://www.example.com', '_blank');

Here, window.open() opens a new browser window with the URL 'https://www.example.com' in a new tab ('_blank'). Developers can also specify window dimensions, features, and other properties as needed.

3. Prompting Users Before Window Close

Developers often require user confirmation before leaving a page, especially when the user has unsaved changes or might lose data. The onbeforeunload event can be employed to prompt users with a confirmation dialog before they navigate away from the current page.

window.onbeforeunload = function() {
return 'Are you sure you want to leave?';
};

This code will trigger a confirmation dialog with the specified message when the user tries to close the tab or window, providing an opportunity to confirm their action or stay on the current page.

4. Accessing Geolocation

The navigator.geolocation API allows web applications to access the user's geographical location if the user grants permission. By using getCurrentPosition(), developers can retrieve the user's current location coordinates.

navigator.geolocation.getCurrentPosition(function(position) {
console.log('Latitude:', position.coords.latitude);
console.log('Longitude:', position.coords.longitude);
});

Upon user consent, this function retrieves the latitude and longitude coordinates of the user’s device, enabling applications to provide location-based services or personalize content based on the user’s geographical position.

🔋Conclusion

Understanding the Browser Object Model (BOM) in JavaScript is crucial for developing dynamic and interactive web applications. It provides access to various browser-related objects and functionalities, allowing developers to create rich user experiences. By leveraging BOM, developers can manipulate browser windows, handle user interactions, access browser information, and much more, enhancing the overall functionality of web applications.

✌️You can always support me through the website “BuyMeACoffee” — and buy me a cup of coffee✌️
Thank you very much!

Stackademic

Thank you for reading until the end. Before you go:

  • Please consider clapping and following the writer! 👏
  • Follow us on Twitter(X), LinkedIn, and YouTube.
  • Visit Stackademic.com to find out more about how we are democratizing free programming education around the world.

--

--

Web development using React, JavaScript, HTML, CSS, Git, MongoDB, Handlebars, SCSS, and PUG. Also, I am actively learning how to work in Java and OracleDB.