The BOM (Browser Object Model) is a set of interfaces and objects in JavaScript that allow interaction with the browser and its environment (such as the window, the screen, the URL, and the history).
Unlike the Document Object Model (DOM), which manipulates the content of the web page, the BOM is focused on interacting with the browser and its features.
The main elements that make up the Browser Object Model (BOM) are:
BOM Element | Description |
---|---|
window | Represents the browser window |
navigator | Provides information about the browser |
screen | Represents the user’s screen |
location | Contains information about the current URL (allows changing it) |
document | Represents the content of the web page |
history | Allows manipulating the browser’s navigation history, |
console | Provides methods for debugging |
performance | Provides access to performance information of the web page |
applicationCache | Allows access to the application cache (deprecated) |
Let’s look at each of them in detail 👇
Browser Window window
The global object window
represents the browser window and is the root of the BOM. Many properties and methods of the BOM can be accessed through window
.
Command | Description |
---|---|
window.innerHeight | Gets the height of the browser window. |
window.innerWidth | Gets the width of the browser window. |
window.resizeTo(500, 500) | Resizes the window to 500x500 pixels. |
window.status = "Loading..." | Modifies the text in the status bar. |
window.setDefaultStatus("Ready") | Sets the default text of the status bar. |
For example,
console.log(window.innerHeight); // Height of the window
console.log(window.innerWidth); // Width of the window
Browser Information navigator
The navigator
object provides information about the browser being used. Among other things, it can be used to detect the name and version of the browser, as well as the platform on which it is running.
Command | Description |
---|---|
navigator.userAgent | Shows information about the browser. |
navigator.platform | Shows the platform on which the browser is running. |
For example,
console.log(navigator.userAgent); // Browser information
console.log(navigator.platform); // Platform on which it is running
Screen Information screen
The screen
object represents the user’s screen and provides information about its size and resolution.
Command | Description |
---|---|
screen.width | Gets the width of the screen. |
screen.height | Gets the height of the screen. |
screen.availWidth | Available width of the screen. |
screen.availHeight | Available height of the screen. |
screen.pixelDepth | Pixel depth of the screen. |
For example,
console.log(screen.width); // Width of the screen
console.log(screen.height); // Height of the screen
console.log(screen.availWidth); // Available width of the screen
console.log(screen.availHeight); // Available height of the screen
console.log(screen.pixelDepth); // Pixel depth of the screen
URL Information location
The location
object provides information about the current URL and allows redirection to other pages.
Command | Description |
---|---|
location.href | Gets the current URL. |
location.host | Gets the host of the current URL. |
location.pathname | Gets the path of the current URL. |
location.href = "https://www.example.com" | Redirects to another page. |
For example,
console.log(location.href); // Current URL
console.log(location.host); // Host of the current URL
console.log(location.pathname); // Path of the current URL
location.href = "https://www.example.com"; // Redirect to another page
Document document
The document
object represents the web page loaded in the browser window and is the root of the DOM.
Command | Description |
---|---|
document.title | Gets the title of the page. |
document.body | Gets the body of the page. |
For example,
// Title of the page
console.log(document.title);
// Body of the page
console.log(document.body);
// Element with the ID "myElement"
console.log(document.getElementById("myElement"));
Through document
, we can access the elements of the web page and manipulate its content. We’ve seen this extensively in the rest of the articles dedicated to the DOM.
Browser History history
JavaScript allows modifying the browser’s history using the history
object. For example, we can add a new entry to the browser’s history using the pushState()
method.
Command | Description |
---|---|
history.pushState(...) | Adds a new entry to the history with a modified URL. |
history.replaceState(...) | Replaces the current entry in the history with a new URL. |
For example,
// adds a new entry in the browser's history
// with a URL containing the parameter `page` with the value `1`.
history.pushState({page: 1}, "Title", "?page=1");
// replaces the current entry in the history with a new entry
// with a URL containing the parameter `page` with the value `2`.
history.replaceState({page: 2}, "Title", "?page=2");
Performance Information performance
The performance
object provides access to information related to the performance of the web page, such as load times and navigation events.
Command | Description |
---|---|
performance.now() | Returns the time in milliseconds since the navigation or loading of the page started. |
performance.timing | Provides an object with details about navigation and loading times of the page. |
performance.getEntries() | Returns all available performance entries. |
For example, to measure the exact time elapsed since the page was loaded:
// Time in milliseconds since the loading started
console.log(performance.now());
This is useful for measuring the page load speed, analyzing application performance, and making optimizations based on the collected data.
Application Cache applicationCache
The applicationCache
object allows managing the web application’s cache, facilitating the storage of resources (such as CSS files, images, and JavaScript) for offline use.
Command | Description |
---|---|
applicationCache.status | Returns the current status of the application cache. |
applicationCache.update() | Forces the application cache to update. |
For example, to check the status of the application cache:
// Cache status (for example, 0 = no cache, 1 = cache in use)
console.log(applicationCache.status);
We can also add events related to the cache status, such as cached
, updateready
, and error
.
applicationCache.addEventListener("event", callback)
This object is deprecated (in favor of Service Workers), although it may still be useful for older applications that need access to the browser’s cache.