Angular 101: What Are DOM and BOM

What Are DOM and BOM?

1. Quick Answer

English

The Document Object Model (DOM) is a programming interface for web documents that represents the structure of HTML and XML documents. It allows scripts to access and manipulate the document’s content, structure, and styling.

The Browser Object Model (BOM) is a representation of the browser’s window and provides methods and properties to interact with the browser itself, such as controlling browser windows, accessing browser history, and managing events.

Chinese

文档对象模型 (DOM) 是一种用于网页文档的编程接口,表示 HTML 和 XML 文档的结构。它允许脚本访问和操作文档的内容、结构和样式。

浏览器对象模型 (BOM) 是浏览器窗口的表示,并提供方法和属性来与浏览器本身进行交互,例如控制浏览器窗口、访问浏览器历史记录和管理事件。

2. 5Ws Analysis

1. What is DOM? / 什么是 DOM?

  • English: DOM represents the hierarchical structure of a document and allows scripts to access and update its content, structure, and styles.
  • 中文: DOM 表示文档的层次结构,并允许脚本访问和更新其内容、结构和样式。

2. What is BOM? / 什么是 BOM?

  • English: BOM provides methods and properties to interact with the browser window, such as controlling window size, managing cookies, or accessing the browser history.
  • 中文: BOM 提供与浏览器窗口交互的方法和属性,例如控制窗口大小、管理 Cookie 或访问浏览器历史记录。

3. Who uses DOM and BOM? / 谁使用 DOM 和 BOM?

  • English: Web developers and designers use DOM and BOM to create interactive web pages and handle browser-level manipulations.
  • 中文: Web 开发人员和设计师使用 DOM 和 BOM 来创建交互式网页并处理浏览器级别的操作。

4. When are DOM and BOM used? / 什么时候使用 DOM 和 BOM?

  • English: DOM is used whenever manipulating or accessing a webpage’s content, such as updating text dynamically. BOM is used when interacting with the browser, like opening new windows or navigating through history.
  • 中文: 当需要操作或访问网页内容时会使用 DOM,例如动态更新文本。BOM 用于与浏览器交互时,例如打开新窗口或浏览历史记录。

5. Why are DOM and BOM important? / 为什么 DOM 和 BOM 重要?

  • English: DOM and BOM are fundamental for web development. DOM enables dynamic content updates and user interactions, while BOM allows control over the browser’s features and behaviors.
  • 中文: DOM 和 BOM 是 Web 开发的基础。DOM 使动态内容更新和用户交互成为可能,而 BOM 允许对浏览器的功能和行为进行控制。

3. Code Example

DOM Manipulation Example / DOM 操作示例

English: A basic example of using the DOM to update a paragraph’s text content.
中文: 使用 DOM 更新段落文本内容的基本示例。

<!DOCTYPE html>
<html>
<body>

<p id="demo">Hello World!</p>

<button type="button" onclick="changeText()">Change Text</button>

<script>
// English: Function to change paragraph content
// 中文: 更改段落内容的函数
function changeText() {
    document.getElementById("demo").innerHTML = "Text Updated!";
}
</script>

</body>
</html>

BOM Manipulation Example / BOM 操作示例

English: A basic example of using BOM to open a new window.
中文: 使用 BOM 打开新窗口的基本示例。

// English: This function opens a new browser window with the specified URL.
// 中文: 该函数打开一个带有指定 URL 的新浏览器窗口。
function openNewWindow() {
    window.open("https://www.example.com", "_blank", "width=400,height=400");
}

4. Key Points & Tips

English

  • DOM:

    • DOM is mainly used to manipulate the structure and content of web pages.
    • You can access DOM elements using document.getElementById, document.querySelector, and other selectors.
    • DOM events such as click, mouseover, and keydown allow interaction with the user.
  • BOM:

    • BOM focuses on interacting with the browser, like navigating through history (window.history) or working with cookies.
    • Common BOM methods include window.open(), window.close(), and window.location.

Chinese

  • DOM:

    • DOM 主要用于操作网页的结构和内容。
    • 你可以使用 document.getElementByIddocument.querySelector 和其他选择器来访问 DOM 元素。
    • DOM 事件(如 clickmouseoverkeydown)允许与用户交互。
  • BOM:

    • BOM 侧重于与浏览器交互,例如浏览历史记录 (window.history) 或操作 Cookie。
    • 常见的 BOM 方法包括 window.open()window.close()window.location

5. Warning

English

  • When manipulating the DOM, excessive changes or large DOM structures can lead to performance issues, such as slow page loading or UI lag.
  • Using BOM to manipulate windows, like opening too many new tabs, can negatively impact user experience and lead to security issues like pop-up blocking.

Chinese

  • 操作 DOM 时,过多的更改或过大的 DOM 结构可能导致性能问题,如页面加载缓慢或 UI 卡顿。
  • 使用 BOM 操作窗口,如打开过多的新标签页,可能对用户体验产生负面影响,并导致安全问题(如弹出窗口被拦截)。

6. Comparison Table

Feature / 特性 DOM / 文档对象模型 BOM / 浏览器对象模型
Focus / 重点 Document content and structure manipulation / 操作文档内容和结构 Browser window and environment control / 控制浏览器窗口和环境
Methods / 方法 getElementById, querySelector, addEventListener window.open, window.history, window.location
Usage / 用途 Dynamically update content / 动态更新内容 Manage browser interactions / 管理浏览器交互
Events / 事件 DOM events like click, change / DOM 事件如 click, change BOM events like resize, load / BOM 事件如 resize, load
Impact / 影响 Affects webpage only / 仅影响网页 Affects browser behavior / 影响浏览器行为

7. Interview Questions and Answers

  1. Question: What is the main difference between DOM and BOM? / DOM 和 BOM 之间的主要区别是什么?

    • Answer: DOM focuses on the structure and content of the webpage, while BOM deals with the browser’s environment and features. / DOM 侧重于网页的结构和内容,而 BOM 处理浏览器的环境和功能。
  2. Question: How do you access an HTML element using DOM? / 如何使用 DOM 访问 HTML 元素?

    • Answer: You can access an HTML element using methods like document.getElementById or document.querySelector. / 你可以使用 document.getElementByIddocument.querySelector 来访问 HTML 元素。
  3. Question: What are some common BOM methods and properties? / 常见的 BOM 方法和属性有哪些?

    • Answer: Common BOM methods include window.open, window.close, and properties like window.history and window.location. / 常见的 BOM 方法包括 window.openwindow.close,以及 window.historywindow.location 等属性。
  4. Question: How can you modify the document title using DOM? / 如何使用 DOM 修改文档标题?

    • Answer: You can modify the document title by setting document.title = "New Title". / 你可以通过设置 document.title = "新标题" 来修改文档标题。
  5. Question: How does BOM handle browser history? / BOM 如何处理浏览器历史记录?

    • Answer: BOM provides the window.history object, which allows you to access and manipulate the browser’s history with methods like back, forward, and go. / BOM 提供了 window.history 对象,可以使用 backforwardgo 等方法来访问和操作浏览器的历史记录。

8. Summary

English

  • The DOM is crucial for manipulating a webpage’s content, structure, and style. It enables dynamic content updates and interactivity through JavaScript.
  • The BOM allows developers to interact with the browser’s environment, such as opening new windows or managing the browser history.
  • Together, DOM and BOM provide comprehensive control over web applications, enabling robust front-end development.

Chinese

  • DOM 对于操作网页的内容、结构和样式至关重要。它通过 JavaScript 实现动态内容更新和交互。
  • BOM 允许开发人员与浏览器环境交互,例如打开新窗口

或管理浏览器历史记录。

  • DOM 和 BOM 共同为 Web 应用程序提供了全面的控制,支持强大的前端开发。

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *