--- title: webNavigation.onDOMContentLoaded slug: Mozilla/Add-ons/WebExtensions/API/webNavigation/onDOMContentLoaded tags: - API - onDOMContentLoaded - webNavigation - webNavigation.onDOMContentLoaded translation_of: Mozilla/Add-ons/WebExtensions/API/webNavigation/onDOMContentLoaded ---
{{AddonSidebar()}}

在页面中触发DOMContentLoaded 事件时触发。此时,文档被加载和解析,并且DOM被完全构造,但链接的资源(例如图像,样式表和子框架(subframes))可能尚未被加载。

Syntax

browser.webNavigation.onDOMContentLoaded.addListener(
  listener,                   // function
  filter                      // optional object
)
browser.webNavigation.onDOMContentLoaded.removeListener(listener)
browser.webNavigation.onDOMContentLoaded.hasListener(listener)

事件有三个方法:

addListener(callback)
为此事件添加监听方法.
removeListener(listener)
停止监听此事件. listener 参数为需要移出的监听方法.
hasListener(listener)
检测是否有 listener 被注册在事件上. 如果有返回 true , 否则返回false .

addListener syntax

参数

callback

为当此事件发生是需要被调用的函数. 该函数将传递以下参数:

details
object. 有关导航(navigation)事件的详细信息.
filter{{optional_inline}}

object. 包含单个属性 url 的对象,  这是一个  {{WebExtAPIRef("events.UrlFilter")}} 数组对象. 如果包含此参数,则该事件将仅触发转换为与数组中至少一个UrlFilter匹配的URL。 在数组中。如果您省略此参数,则该事件将触发所有转换。

Additional objects

details

tabId
integer. The ID of the tab in which the navigation has occurred.
url
string. The URL to which the given frame has navigated.
processId
integer. The ID of the process in which this tab is being rendered.
frameId
integer. Frame in which the navigation is occurring. 0 indicates that navigation happens in the tab's top-level browsing context, not in a nested iframe. A positive value indicates that navigation happens in a nested iframe. Frame IDs are unique for a given tab and process.
timeStamp
number. The time at which DOMContentLoaded was fired, in milliseconds since the epoch.

Browser compatibility

{{Compat("webextensions.api.webNavigation.onDOMContentLoaded")}}

Examples

Logs the target URLs for onDOMContentLoaded, if the target URL's hostname contains "example.com" or starts with "developer".

var filter = {
  url:
  [
    {hostContains: "example.com"},
    {hostPrefix: "developer"}
  ]
}

function logOnDOMContentLoaded(details) {
  console.log("onDOMContentLoaded: " + details.url);
}

browser.webNavigation.onDOMContentLoaded.addListener(logOnDOMContentLoaded, filter);

{{WebExtExamples}}

Acknowledgements

This API is based on Chromium's chrome.webNavigation API. This documentation is derived from web_navigation.json in the Chromium code.

Microsoft Edge compatibility data is supplied by Microsoft Corporation and is included here under the Creative Commons Attribution 3.0 United States License.