大网数据广告活动
Web 标准与前端开发

NavigationPrecommitController: addHandler() method

· 52CE 我爱测

The **`addHandler()`** method of the NavigationPrecommitController interface allows you to dynamically add a handler callback function in precommit code, which will then be run after the navigation has committed.

Navigation API

The addHandler() method of the NavigationPrecommitController interface allows you to dynamically add a handler callback function in precommit code, which will then be run after the navigation has committed.

This is useful when the navigation workflow depends on information that is not know until precommit code has started running. If the precommit and (post-commit) handler are independent, the handler can be specified in the in the options.handler argument passed to NavigateEvent.intercept().

Syntax

addHandler(handler);

Parameters

  • handler
    • : A callback function that defines the post-commit navigation handling behavior should be; it returns a promise.

      The handler callback is invoked as though it was passed to the NavigateEvent.intercept() method, and will run after the Navigation.currentEntry property has been updated.

Return value

None (undefined).

Exceptions

  • InvalidStateError DOMException
    • : Thrown if:
      • The originating NavigateEvent was not intercepted or has been canceled.
      • The Document is not fully active.
  • SecurityError DOMException
    • : Thrown if the event Event/isTrusted attribute is false.

Examples

For more examples see NavigationPrecommitController.

Basic usage

This example shows a precommitHandler implementation that fetches data for a page and uses addHandler() to add different handlers based on the page type (the implementations of the fetchConfig, setupVideoPlayer() and setupArticleView() are not given).

navigation.addEventListener("navigate", (event) => {
  event.intercept({
    async precommitHandler(controller) {
      const pageData = await fetchConfig();
      if (pageData.type === "video") {
        controller.addHandler(() => setupVideoPlayer());
      } else {
        controller.addHandler(() => setupArticleView());
      }
    },
  });
});

Specifications

Browser compatibility

See also

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。