Language: EN

javascript-browser-object-model

What is the BOM in JavaScript

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 ElementDescription
windowRepresents the browser window
navigatorProvides information about the browser
screenRepresents the user’s screen
locationContains information about the current URL (allows changing it)
documentRepresents the content of the web page
historyAllows manipulating the browser’s navigation history,
consoleProvides methods for debugging
performanceProvides access to performance information of the web page
applicationCacheAllows 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.

CommandDescription
window.innerHeightGets the height of the browser window.
window.innerWidthGets 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.

CommandDescription
navigator.userAgentShows information about the browser.
navigator.platformShows 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.

CommandDescription
screen.widthGets the width of the screen.
screen.heightGets the height of the screen.
screen.availWidthAvailable width of the screen.
screen.availHeightAvailable height of the screen.
screen.pixelDepthPixel 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.

CommandDescription
location.hrefGets the current URL.
location.hostGets the host of the current URL.
location.pathnameGets 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.

CommandDescription
document.titleGets the title of the page.
document.bodyGets 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")); 

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.

CommandDescription
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.

CommandDescription
performance.now()Returns the time in milliseconds since the navigation or loading of the page started.
performance.timingProvides 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.

CommandDescription
applicationCache.statusReturns 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.