DOM & Browser APIs

Browser Object Model

The BOM gives JavaScript access to the browser environment beyond the document — the current URL, navigation history, screen metrics, geolocation, clipboard, timers, and the global window object itself.

What is the BOM in JavaScript?

The Browser Object Model is everything the browser exposes outside the page content: window (the global object and viewport), location (the URL), history (back, forward and pushState), navigator (browser and device info, clipboard, geolocation) and screen. It's how you read or change the URL, open windows and detect capabilities.

location.href;                     // full URL
new URL(location.href).searchParams.get("q");
history.pushState({}, "", "/new-path");   // change URL without reload
window.innerWidth;                 // viewport width
await navigator.clipboard.writeText("copied!");
window.matchMedia("(prefers-color-scheme: dark)").matches;

Key BOM Objects

window
  • window.innerWidth / innerHeight
  • window.devicePixelRatio
  • window.open(url, target)
  • window.scrollTo(x, y)
  • window.scrollBy(x, y)
  • window.print()
location
  • location.href (full URL)
  • location.pathname
  • location.search (?q=…)
  • location.hash (#anchor)
  • location.assign(url)
  • location.replace(url)
  • location.reload()
history
  • history.back()
  • history.forward()
  • history.go(n)
  • history.pushState(state, title, url)
  • history.replaceState(…)
  • history.length
navigator
  • navigator.userAgent
  • navigator.language
  • navigator.onLine
  • navigator.geolocation
  • navigator.clipboard
  • navigator.share(data)
screen
  • screen.width / height
  • screen.availWidth / availHeight
  • screen.colorDepth
  • screen.pixelDepth
  • screen.orientation
Timers
  • setTimeout(fn, ms)
  • clearTimeout(id)
  • setInterval(fn, ms)
  • clearInterval(id)
  • requestAnimationFrame(fn)
  • cancelAnimationFrame(id)

SPA Navigation with History API

pushState — add history entry

history.pushState({ page: 'home' }, '', '/home');
// URL changes, NO reload, user can go back

replaceState — no new entry

history.replaceState({ page: 'home' }, '', '/home');
// URL changes, replaces current, can NOT go back

popstate — handle back/forward

window.addEventListener('popstate', (e) => {
  console.log('state:', e.state); // { page: 'home' }
  renderPage(e.state?.page);
});

URLSearchParams

const p = new URLSearchParams(location.search);
console.log(p.get('q'));    // 'javascript'
p.set('sort', 'newest');
history.pushState(null, '', '?' + p);

Common Pitfalls

Using location.reload() on every state change — causes full page reload and loses in-memory state
Use history.pushState() to update the URL without reloading for SPA-style navigation
Using window.open() inside async callbacks — browsers block popups not triggered directly by user gestures
Call window.open() synchronously inside a click handler, before any await
Polling with setInterval — missed ticks if the tab is hidden, accumulates lag
Use requestAnimationFrame for visual updates and the Page Visibility API (visibilitychange) for pausing work
Reading navigator.userAgent to detect features — unreliable, fragile, can be spoofed
Use feature detection (typeof AudioContext !== 'undefined') or the Permissions API instead

Interactive BOM Explorer

Code Editor

<pre id="out"></pre>
<button id="push">pushState("?tab=2")</button>
<button id="copy">Copy URL</button>

Preview

Browser Object Model — step by step

1 / 4
1

The window Object

The window object is the global object in a browser context. Every global variable and function you declare becomes a property of window. It also provides access to the entire BOM: location, history, navigator, screen, and timers.

1 of 4
💡window Is Optional in Global Scope
Because all globals live on window, you can call setTimeout, alert, or fetch without prefixing them with window. However, when referencing globals from inside modules or closures, being explicit with window.setTimeout makes the intent clear and avoids shadowing bugs.

🎯 Quick Check

Which API lets a single-page app update the URL in the address bar without triggering a full page reload?

The Browser Object Model: summary

Key points

  • window is the global object in browsers; globalThis works in every environment.
  • Use URL and URLSearchParams to read and build query strings safely.
  • pushState updates the URL; listen to popstate to handle Back and Forward.
  • Feature-detect ("clipboard" in navigator) instead of sniffing the user agent.

Frequently asked questions

›How do I get query parameters from the URL?

new URLSearchParams(location.search).get("name").