1. HTML Parser
The HTML parser in JavaScript is responsible for reading and interpreting HTML code, converting it into a structured format—usually a DOM (Document Object Model) tree—that can be manipulated by JavaScript. This process, known as parsing, is essential for browsers and tools to understand and work with HTML content effectively.
Key Points:
- It starts parsing HTML from top to bottom.
- Converts tags into nodes and creates a DOM tree.
- Works closely with the CSS and JavaScript engines.
- JavaScript can interrupt parsing using
<script>tags.
Example:
// This code relies on the DOM created by the HTML parser
console.log(document.body.children[0].innerText); // Logs text of first child of body
2. Document Object Model (DOM)
The DOM is a programming interface that represents the structure of an HTML or XML document as a tree of objects. It allows you to read, manipulate, and modify the content and structure of web pages using JavaScript.
Key Points:
- Each HTML element is a node in the DOM tree.
- It is live, meaning changes reflect immediately on the page.
- Supports methods like
getElementById,querySelector, etc.
Example:
// Changes heading text using DOM
document.querySelector('h1').innerText = 'Welcome to DOM World!';
4. Browser Object Model (BOM)
The Browser Object Model (BOM) provides JavaScript access to browser-specific features like navigation, screen size, and browser history. It is separate from the content of the page.
Key Points:
- Not standardized like DOM (varies across browsers).
- Interacts with browser components like
navigator,location,history, etc. - Allows you to control the browser window/tab.
Example:
console.log('Current URL:', window.location.href);
console.log('Browser Info:', window.navigator.userAgent);
5. Window Object
The Window object is the global object for JavaScript in the browser. Every
global variable or function is actually a property or method of window.
Key Points:
- It includes the BOM and gives access to the DOM.
- Represents the browser tab or window itself.
- Functions like
alert()orsetTimeout()are methods ofwindow.
Example:
window.alert('Hello from the Window object!');
console.log('Window dimensions:', window.innerWidth, 'x', window.innerHeight);
6. Comparison Table: Window vs BOM vs DOM vs HTML Parser
| Aspect | Window | BOM | DOM | HTML Parser |
|---|---|---|---|---|
| Definition | Global object for browser tab | Browser feature interface | Page content interface | Engine to convert HTML to DOM |
| Contains | DOM + BOM | navigator, screen, history | document, elements | HTML to tree converter |
| Use Case | Global browser-level control | Browser-related tasks | Manipulate HTML content | Builds DOM from HTML |
| Examples | alert(), setTimeout() | window.location.href | document.querySelector() | Internal process (not accessed directly) |