L
LLLOS.ai
Learn
L

Ask Your Tutor · Computer Science

How does JavaScript interact with the DOM and BOM?

From NCERT In depth Asked by Anonymous from New York, United States 24 Oct 2026, 8:25 AM IST Updated

In one line

JavaScript uses the DOM to change web pages and the BOM to control the browser itself.

The rule

JavaScript interacts with the Document Object Model (DOM) to change the content, structure, and style of web pages, and with the Browser Object Model (BOM) to control browser features like windows, alerts, and navigation.

There is no single formula, but the main objects are:

  • document (for DOM)
  • window (for BOM)

Let's understand it simply

Imagine your web page is like a big notice board in your school. The DOM is like a map of everything on that board: every notice, photo, and heading has a place and a name. JavaScript is like the teacher with a marker pen. The teacher can add new notices, change the words, or remove old ones — all without taking down the whole board. That’s how JavaScript changes the DOM.

Now, the BOM is like the room where the board is kept. The teacher can open or close windows, turn on the fan, or even move the board to another room. JavaScript uses the BOM to do things like open a new browser tab, show a pop-up, or check the browser’s size.

DOM vs BOM
DOMBOM
What it controlsWeb page content, structure, styleBrowser features (window, history, alerts)
Main objectdocumentwindow
ExampleChange text on pageShow alert box
Standard?Yes (W3C)No (browser-specific)

How it works

  1. The browser loads your web page and creates the DOM (a tree-like structure of all HTML elements).
  2. JavaScript accesses the DOM using the document object (for example, document.getElementById('header')).
  3. JavaScript changes the DOM by adding, removing, or editing elements, attributes, or styles (like changing text or color).
  4. The browser updates the page to show these changes instantly to the user.
  5. JavaScript uses the BOM through the window object to control browser features (like opening a new window: window.open(), or showing an alert: window.alert('Hello!')).
  6. Events (like clicks or key presses) can trigger JavaScript to interact with both DOM and BOM, making the page interactive.
How JavaScript Interacts with DOM and BOM
  1. Browser loads page and creates DOM
  2. JavaScript accesses DOM with 'document'
  3. JavaScript changes DOM (content, style, structure)
  4. Browser updates the web page
  5. JavaScript uses BOM via 'window' (alerts, open new tab)

The story behind it

The DOM was created in the late 1990s by the World Wide Web Consortium (W3C). Before the DOM, web pages were static — you could only see what was written in the HTML. People wanted web pages to react to users, like showing messages or hiding parts of the page. The DOM gave a standard way for JavaScript to talk to the page.

The BOM is not an official standard, but browser makers like Netscape and Microsoft added it so JavaScript could control things outside the page, like the browser window or history. This made web pages more powerful and interactive.

Where you see it in real life

  • Changing a cricket score live: The score on a sports website updates without reloading the page. JavaScript changes the DOM to show the new score.
  • Form validation: When you fill a form and forget a field, a red warning appears. JavaScript checks the input and updates the DOM to show the message.
  • Pop-up alerts: When you click a button and see a message box (“Are you sure?”), JavaScript uses the BOM (window.alert).
  • Opening new tabs/windows: Clicking a link that opens in a new window uses the BOM (window.open).
  • Changing styles on hover: When you move your mouse over a button and it changes color, JavaScript updates the DOM’s style.
  • Checking browser size: Responsive sites adjust layout based on window size, using BOM properties like window.innerWidth.

Why it matters

  • Interactive websites: All modern websites use DOM and BOM to make pages lively and user-friendly.
  • Web apps: Apps like online banking, shopping, or games rely on DOM and BOM manipulation.
  • Job skills: Every web developer must know how to use DOM and BOM with JavaScript.
  • Exams and projects: Questions on DOM/BOM are common in CBSE and other board exams, and in practicals.
  • Building blocks: Later topics like AJAX, frameworks (React, Angular), and advanced scripting all build on these basics.

Worked example

Let’s say you want to change the text of a paragraph with id 'demo' and show an alert when a button is clicked.

 
Given: HTML element <p id="demo">Hello</p> and a button <button onclick="changeText()">Click me</button>
Step 1: Write the JavaScript function
function changeText() {
  document.getElementById('demo').innerText = 'Namaste!'; // DOM
  window.alert('Text changed!'); // BOM
}
Step 2: User clicks the button
Step 3: JavaScript runs changeText()
Step 4: The paragraph text changes to 'Namaste!' (DOM)
Step 5: An alert box pops up saying 'Text changed!' (BOM)

Common mistakes

  • Mixing up DOM and BOM: DOM is for page content; BOM is for browser features. Remember: document (DOM), window (BOM).
  • Forgetting to wait for the page to load: If you run JavaScript before the DOM is ready, it may not find elements. Use window.onload or put scripts at the end of the HTML.
  • Using wrong property: innerText vs innerHTML. innerText changes only text; innerHTML can add HTML tags too.
  • Not handling errors: If the element id is wrong, getElementById returns null, and your code will fail.

Remember it

DOM changes the page; BOM controls the browser. Use document for DOM, window for BOM.

Check yourself

What is the main difference between DOM and BOM?

DOM controls the web page content; BOM controls browser features.

Which JavaScript object is used to change the text of an HTML element?

The 'document' object (DOM) is used, for example, document.getElementById().

Why do we use window.alert() in JavaScript?

To show a pop-up message using the BOM.

Why should you wait for the page to load before running DOM code?

Because if the DOM is not ready, JavaScript may not find the elements and will fail.

Quick recap

  • JavaScript uses the DOM to change web page content, structure, and style.
  • JavaScript uses the BOM to control browser features like alerts and windows.
  • The DOM is accessed with the 'document' object; the BOM with 'window'.
  • DOM manipulation makes websites interactive and dynamic.
  • BOM lets JavaScript interact with the browser itself, not just the page.
Written by the LLOS.ai tutor, checked against NCERT chapters. Asked by Anonymous from New York, United States on 24 Oct 2026, 8:25 AM IST