The Browser Object Model (BOM) allows JavaScript to interact with the browser itself, not just the HTML page. It provides objects and methods to work with the browser window, URL, history, screen, and more.
While the DOM focuses on web page elements, the BOM focuses on the browser environment.
? BOM = Browser environment APIs
window – The global browser object; parent of all other BOM objects.screen – Information about the user's screen (width, height, etc.).location – Information about the current URL and navigation.history – Browser session history (back, forward navigation).navigator – Information about the browser/application (user agent, etc.).alert(), confirm(), prompt().setTimeout(), setInterval().document.cookie.In browsers, global variables and functions automatically become properties of the window object. The BOM is centered around this global window object.
window.innerWidth – Width of the content area of the window.window.innerHeight – Height of the content area.window.alert() – Shows an alert dialog box.window.location – Access/modify the current URL.document (HTML elements).window and browser environment.window.document and document refer to the same DOM object.
window.alert("Hello from BOM!");
console.log(window.innerWidth); // Width of the browser window
console.log(window.document === document); // true
alert("Welcome to my page!");
const isSure = confirm("Are you sure you want to continue?");
console.log("User clicked:", isSure ? "OK" : "Cancel");
const name = prompt("What is your name?");
if (name !== null) {
console.log("Hello, " + name + "!");
}
location
if (confirm("Do you want to visit OpenAI?")) {
location.href = "https://www.openai.com";
}
setTimeout and setInterval
function showMessage() {
console.log("This runs after 2 seconds");
}
setTimeout(showMessage, 2000); // Run once after 2 seconds
let count = 0;
const intervalId = setInterval(() => {
console.log("Tick:", ++count);
if (count === 5) {
clearInterval(intervalId); // Stop after 5 ticks
}
}, 1000);
alert() opens a modal dialog with an OK button. The user must close it to continue.confirm() returns true if the user clicks OK, or false if they click Cancel.prompt() returns the typed string, or null if the user cancels.location.href changes the current page URL, effectively redirecting the user.setTimeout() schedules code to run once after a delay, without blocking the page.setInterval() repeats code at a fixed time interval until clearInterval() is called.Open your browser's Developer Tools → Console to see the log messages from these examples and better understand how BOM methods behave in real time.
alert() or custom UIs.confirm().prompt() (for demos/tests).location.href or location.reload().setTimeout/setInterval.screen.width.window is optional in many cases: alert() is the same as window.alert().console.log() over alert() for debugging, so you don't block the UI.null when using prompt() to handle Cancel gracefully.alert, confirm, prompt) in production apps. Use custom modals instead.setTimeout and setInterval carefully to avoid memory leaks; clear intervals when no longer needed.alert() welcoming users to your website.location.href to redirect users to another page when they click “OK” in a confirm() dialog.screen.width and screen.height.prompt() and then display a personalized greeting in the console.navigator.userAgent to inspect browser information.setInterval() to increase a number every second, and stop it after 10 seconds.