BOM
The BOM (Browser Object Model) lets JavaScript talk to the browser itself — not the page content, but the browser window around it. It gives you access to things like the URL, screen size, browser history, and storage.
The BOM is not part of the JavaScript language standard,
but all modern browsers support it. The top-level object
of the BOM is window.
// The window object is the global object in browsers
console.log(window); // shows all browser properties and methods
// Global variables and functions become properties of window
var name = "Alice";
console.log(window.name); // Output: Alice
The window object is the root of the BOM.
It represents the browser tab. Common properties and
methods include innerWidth,
innerHeight, alert(),
confirm(), setTimeout(), and
setInterval().
// Viewport size
console.log(window.innerWidth); // e.g. 1440
console.log(window.innerHeight); // e.g. 900
// Open a popup
window.alert("Hello!");
// Run code after a delay (milliseconds)
window.setTimeout(function () {
console.log("1 second later");
}, 1000);
// Run code repeatedly
const id = window.setInterval(function () {
console.log("every 2 seconds");
}, 2000);
// Stop the interval
window.clearInterval(id);
window.history gives access to the
browser's session history — the list of pages the user
has visited in the current tab. You can navigate back and
forward without a page reload.
// Go back one page (same as clicking the Back button)
history.back();
// Go forward one page
history.forward();
// Go a specific number of steps (-2 = two pages back)
history.go(-2);
// Total number of entries in the history stack
console.log(history.length);
window.screen gives information about the
user's physical screen (monitor). This is different from
the browser viewport size (window.innerWidth).
// Full screen resolution
console.log(screen.width); // e.g. 1920
console.log(screen.height); // e.g. 1080
// Available area (excluding OS taskbar)
console.log(screen.availWidth);
console.log(screen.availHeight);
// Color depth in bits
console.log(screen.colorDepth); // e.g. 24
window.location holds the current page URL
and lets you navigate to a new URL from JavaScript.
// Full URL
console.log(location.href);
// e.g. "https://example.com/page?q=hello#section"
// Just the domain
console.log(location.hostname); // e.g. "example.com"
// Path after the domain
console.log(location.pathname); // e.g. "/page"
// Query string
console.log(location.search); // e.g. "?q=hello"
// Hash fragment
console.log(location.hash); // e.g. "#section"
// Navigate to a new page
location.href = "https://example.com";
// Reload the current page
location.reload();
localStorage lets you store key-value pairs
in the browser with no expiry. Data
stays even after the tab or browser is closed. It can
store up to about 5 MB per origin.
// Save a value
localStorage.setItem("theme", "dark");
// Read a value
const theme = localStorage.getItem("theme");
console.log(theme); // Output: dark
// Remove one item
localStorage.removeItem("theme");
// Clear everything
localStorage.clear();
// Storing objects (must convert to JSON string)
const user = { name: "Alice", age: 30 };
localStorage.setItem("user", JSON.stringify(user));
// Reading back an object
const stored = JSON.parse(localStorage.getItem("user"));
console.log(stored.name); // Output: Alice
sessionStorage works exactly like
localStorage but data is
cleared when the tab is closed. It is
useful for temporary data that should not persist between
browser sessions.
// Save a value for the session
sessionStorage.setItem("step", "2");
// Read the value
console.log(sessionStorage.getItem("step")); // Output: 2
// Remove it
sessionStorage.removeItem("step");
// Data is automatically gone when the tab closes
Key difference: localStorage data persists
forever (until cleared), while
sessionStorage data is lost when the tab is
closed.
- The BOM lets JavaScript interact with the browser beyond the page content.
- window is the global object and the root of the BOM.
- history lets you navigate back and forward in the session history.
- navigator provides information about the browser and device.
- screen gives the physical screen dimensions.
- location holds the current URL and lets you navigate to another page.
- Cookies are small browser-stored values sent to the server with each request.
- localStorage stores data permanently (until cleared) in the browser.
- sessionStorage stores data only for the current tab session.