diff --git a/2-ui/5-loading/01-onload-ondomcontentloaded/article.md b/2-ui/5-loading/01-onload-ondomcontentloaded/article.md
index 322517af39..8c8817139a 100644
--- a/2-ui/5-loading/01-onload-ondomcontentloaded/article.md
+++ b/2-ui/5-loading/01-onload-ondomcontentloaded/article.md
@@ -1,39 +1,39 @@
-# Page: DOMContentLoaded, load, beforeunload, unload
+# 页面生命周期:DOMContentLoaded,load,beforeunload,unload
-The lifecycle of an HTML page has three important events:
+HTML 页面的生命周期有三个重要事件:
-- `DOMContentLoaded` -- the browser fully loaded HTML, and the DOM tree is built, but external resources like pictures `` and stylesheets may be not yet loaded.
-- `load` -- not only HTML is loaded, but also all the external resources: images, styles etc.
-- `beforeunload/unload` -- the user is leaving the page.
+- `DOMContentLoaded` —— 浏览器完成全部 HTML 的加载,并构建 DOM 树,但像 `` 和样式这样的外部资源可能还没有加载完成。
+- `load` —— 浏览器加载完所有资源,包括 HTML 文档,图像,样式等。
+- `beforeunload/unload` —— 当用户离开页面时。
-Each event may be useful:
+每个事件都是有用的:
-- `DOMContentLoaded` event -- DOM is ready, so the handler can lookup DOM nodes, initialize the interface.
-- `load` event -- external resources are loaded, so styles are applied, image sizes are known etc.
-- `beforeunload` event -- the user is leaving: we can check if the user saved the changes and ask them whether they really want to leave.
-- `unload` -- the user almost left, but we still can initiate some operations, such as sending out statistics.
+- `DOMContentLoaded` 事件 —— DOM 已经准备好,因此事件处理器可以查找 DOM 节点,并初始化接口。
+- `load` 事件 —— 外部资源加载完成后,我们就可以应用样式表,获取图像大小等。
+- `beforeunload` 事件 —— 用户即将离开:我们可以检查用户是否保存了修改,并询问他是否真的要离开。
+- `unload` 事件 —— 用户几乎已经离开了,但是我们仍然可以启动一些操作,比如发送统计数据。
-Let's explore the details of these events.
+我们探讨一下这些事件的细节。
## DOMContentLoaded
-The `DOMContentLoaded` event happens on the `document` object.
+`DOMContentLoaded` 事件发生在 `document` 对象上。
-We must use `addEventListener` to catch it:
+我们必须使用 `addEventListener` 来监听它:
```js
document.addEventListener("DOMContentLoaded", ready);
-// not "document.onDOMContentLoaded = ..."
+// 不同于“document.onDOMContentLoaded = ...”
```
-For instance:
+例如:
```html run height=200 refresh
```
-In the example above, we first see "Library loaded...", and then "DOM ready!" (all scripts are executed).
+在上面的例子中,我们首先会看到“Library loaded...”,然后才会看到“DOM ready!”(所有脚本都已经执行结束)。
-```warn header="Scripts with `async`, `defer` or `type=\"module\"` don't block DOMContentLoaded"
+```warn header="具有 `async`, `defer` or `type=\"module\"` 属性的脚本不会阻塞 DOMContentLoaded"
+我们[稍后会提到的](info:script-async-defer)脚本属性 `async` 和 `defer`,它们不会阻塞 DOMContentLoaded。[JavaScript 模块](info:modules)的行为和 `defer` 相似,同样也不会阻塞 DOMContentLoaded。
-Script attributes `async` and `defer`, that we'll cover [a bit later](info:script-async-defer), don't block DOMContentLoaded. [JavaScript modules](info:modules) behave like `defer`, they don't block it too.
-
-So here we're talking about "regular" scripts, like ``, or ``.
+所以在这里,我们研究的是“普通”脚本,比如 `` 或者 ``。
```
-### DOMContentLoaded and styles
-External style sheets don't affect DOM, so `DOMContentLoaded` does not wait for them.
+### DOMContentLoaded 和样式
+
+外部样式不会影响 DOM,因此 `DOMContentLoaded` 无需等待它们。
-But there's a pitfall. If we have a script after the style, then that script must wait until the stylesheet loads:
+但有一个陷阱。如果在样式之后有一个脚本,那么该脚本必须等待样式被加载:
```html
```
-The reason is that the script may want to get coordinates and other style-dependent properties of elements, like in the example above. Naturally, it has to wait for styles to load.
+原因是脚本可能希望获取如上述示例所描述的元素坐标和其他与样式相关的属性。因此,它必须等待样式被加载。
-As `DOMContentLoaded` waits for scripts, it now waits for styles before them as well.
+当 `DOMContentLoaded` 等待脚本时,它也在等待脚本之前的样式。
-### Built-in browser autofill
+### 浏览器内置的自动填充
-Firefox, Chrome and Opera autofill forms on `DOMContentLoaded`.
+Firefox、Chrome 和 Opera 都会在 `DOMContentLoaded` 中自动填写表单。
-For instance, if the page has a form with login and password, and the browser remembered the values, then on `DOMContentLoaded` it may try to autofill them (if approved by the user).
+比如,如果页面有一个带有登录和密码的表单,并且浏览器记住了这些值,那么在 `DOMContentLoaded` 触发时,它就可以尝试自动填写它们(如果用户允许的话)。
-So if `DOMContentLoaded` is postponed by long-loading scripts, then autofill also awaits. You probably saw that on some sites (if you use browser autofill) -- the login/password fields don't get autofilled immediately, but there's a delay till the page fully loads. That's actually the delay until the `DOMContentLoaded` event.
+因此,如果 `DOMContentLoaded` 被需要加载很长时间的脚本延迟触发,那么自动填充也在等待。你可能在某些站点上看到过(如果你使用浏览器自动填写) —— 登录/密码字段并不会立即自动填充,而是在页面被完全加载前会延迟填充。这实际上是 `DOMContentLoaded` 事件之前的延迟。
## window.onload [#window-onload]
-The `load` event on the `window` object triggers when the whole page is loaded including styles, images and other resources.
+当包括样式、图像和其他资源的页面被全部加载时,`window` 对象上的 `load` 事件就会被触发。
-The example below correctly shows image sizes, because `window.onload` waits for all images:
+以下示例正确地显示了图像大小,因为 `window.onload` 会等待所有的图像加载完毕:
```html run height=200 refresh
@@ -128,45 +128,44 @@ The example below correctly shows image sizes, because `window.onload` waits for
## window.onunload
-When a visitor leaves the page, the `unload` event triggers on `window`. We can do something there that doesn't involve a delay, like closing related popup windows.
+当访问者离开页面时,`window` 对象上的 `unload` 事件就会被触发。我们可以在那里做一些不涉及延迟的事件,比如关闭相关的弹出窗口。
-The notable exception is sending analytics.
+有一个值得注意的特殊情况那就是发送分析数据。
-Let's say we gather data about how the page is used: mouse clicks, scrolls, viewed page areas, and so on.
+假设我们要收集页面使用情况的数据:鼠标点击、滚动、被查看的页面区域等等。
-Naturally, `unload` event is when the user leaves us, and we'd like to save the data on our server.
+自然地,当用户要离开的时候,我们会使用 `unload` 事件去发送我们想要保存在服务器上的数据。
-There exists a special `navigator.sendBeacon(url, data)` method for such needs, described in the specification .
+这里也有一个特殊的 `navigator.sendBeacon(url, data)` 方法来实现这种需求,请参见 w3c 规范 。
-It sends the data in background. The transition to another page is not delayed: the browser leaves the page, but still performs `sendBeacon`.
+它在后台发送数据,转换到另外一个页面时不会被延迟:浏览器离开页面,但仍然在执行 `sendBeacon`。
-Here's how to use it:
-```js
-let analyticsData = { /* object with gathered data */ };
+下面是它的使用示例:
+```js
+let analyticsData = { /* 收集了数据的对象 */ };
window.addEventListener("unload", function() {
navigator.sendBeacon("/analytics", JSON.stringify(analyticsData));
};
```
-- The request is sent as POST.
-- We can send not only a string, but also forms and other formats, as described in the chapter , but usually it's a stringified object.
-- The data is limited by 64kb.
-
-When the `sendBeacon` request is finished, the browser probably has already left the document, so there's no way to get server response (which is usually empty for analytics).
+- 请求以 POST 方式发送。
+- 我们不仅能发送字符串,还能发送表单以及其他格式的数据,在 章节我们已有说明,但是通常情况下它是一个字符串化的对象。
+- 数据大小限制在 64kb。
-There's also a `keepalive` flag for doing such "after-page-left" requests in [fetch](info:fetch-basics) method for generic network requests. You can find more information in the chapter .
+当 `sendBeacon` 请求完成的时候,浏览器可能已经离开了文档,所以就没办法获取服务器的响应数据(对于统计数据来说通常是空的)。
+还有一个 `keep-alive` 的标志,用于在 [fetch](info:fetch-basics) 方法中为通用的网络请求执行此类“离开页面后(after-page-left)”的请求。你可以在 章节中了解到更多相关信息。
-If we want to cancel the transition to another page, we can't do it here. But we can use another event -- `onbeforeunload`.
+如果我们要取消跳转到另一页面的操作,在这里做不到。但是我们可以用另外一个事件 —— `onbeforeunload`。
## window.onbeforeunload [#window.onbeforeunload]
-If a visitor initiated navigation away from the page or tries to close the window, the `beforeunload` handler asks for additional confirmation.
+如果访问中触发了离开页面的导航或试图关闭窗口,`beforeunload` 处理器将要求提供更多的确认信息。
-If we cancel the event, the browser may ask the visitor if they are sure.
+如果我们取消该事件,浏览器将会询问用户是否确定。
-You can try it by running this code and then reloading the page:
+你可以通过运行这段代码,然后重新加载页面来进行尝试:
```js run
window.onbeforeunload = function() {
@@ -174,9 +173,9 @@ window.onbeforeunload = function() {
};
```
-For historical reasons, returning a non-empty string also counts as canceling the event. Some time ago browsers used show it as a message, but as the [modern specification](https://html.spec.whatwg.org/#unloading-documents) says, they shouldn't.
+由于历史原因,返回非空字符串也算作取消事件。在以前,浏览器通常将其显示为消息,但是根据 [modern specification](https://html.spec.whatwg.org/#unloading-documents) 所述,现在它们并不会显示了。
-Here's an example:
+这里有个例子:
```js run
window.onbeforeunload = function() {
@@ -184,58 +183,59 @@ window.onbeforeunload = function() {
};
```
-The behavior was changed, because some webmasters abused this event handler by showing misleading and annoying messages. So right now old browsers still may show it as a message, but aside of that -- there's no way to customize the message shown to the user.
+它的行为在某种意义上被改变了,因为一些站长通过显示误导性及恶意的信息滥用了这个事件处理器。所以,目前来看一些老旧的浏览器可能仍然显示为消息,但除此之外 —— 没有别的办法自定义显示给用户的消息。
## readyState
-What happens if we set the `DOMContentLoaded` handler after the document is loaded?
+如果在文档加载之后设置 `DOMContentLoaded` 事件处理器会发生什么呢?
-Naturally, it never runs.
+很自然地,它不会被运行。
-There are cases when we are not sure whether the document is ready or not. We'd like our function to execute when the DOM is loaded, be it now or later.
+在某些情况下,我们不确定文档是否已经准备就绪。当 DOM 加载完成时,我们想要执行一些函数,可能是立即执行也可能是稍后执行。
-The `document.readyState` property tells us about the current loading state.
+`document.readyState` 属性为我们提供了一些关于当前加载状态的信息。
-There are 3 possible values:
+它有三个可能的值:
-- `"loading"` -- the document is loading.
-- `"interactive"` -- the document was fully read.
-- `"complete"` -- the document was fully read and all resources (like images) are loaded too.
+- `“loading”` —— 文档正在被加载。
+- `“interactive”` —— 文档被全部读取。
+- `“complete”` —— 文档被全部读取,并且所有的资源(图像之类的)都被加载。
-So we can check `document.readyState` and setup a handler or execute the code immediately if it's ready.
+因此我们可以检查 `document.readyState` 并设置一个处理器,或在代码准备就绪时立即执行它。
-Like this:
+就像这样:
```js
function work() { /*...*/ }
if (document.readyState == 'loading') {
- // loading yet, wait for the event
+ // 正在加载,等待事件
document.addEventListener('DOMContentLoaded', work);
} else {
- // DOM is ready!
+ // DOM 已经准备就绪!
work();
}
```
-There's also `readystatechange` event that triggers when the state changes, so we can print all these states like this:
+还有一个 `readystatechange` 事件,当状态发生变化时触发,因此我们可以打印所有这些状态,就像这样:
```js run
-// current state
+// 当前状态
console.log(document.readyState);
-// print state changes
+// 状态改变时打印它
document.addEventListener('readystatechange', () => console.log(document.readyState));
```
-The `readystatechange` event is an alternative mechanics of tracking the document loading state, it appeared long ago. Nowadays, it is rarely used.
+`readystatechange` 事件是跟踪文档加载状态的另一种机制,它很早就存在了。现在则很少被使用。
-Let's see the full events flow for the completeness.
+但是为了完整起见,我们继续讨论一下它的全部事件。
-Here's a document with `