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

NavigationPrecommitController: redirect() method

· 52CE 我爱测

The **`redirect()`** method of the NavigationPrecommitController interface redirects the browser to a specified URL and specifies history behavior and any desired state information.

Navigation API

The redirect() method of the NavigationPrecommitController interface redirects the browser to a specified URL and specifies history behavior and any desired state information.

Syntax

redirect(url, options)

Parameters

  • url
    • : The URL to redirect to.
  • options
    • : An options object, the properties of which can include:
      • state
        • : Contains any state information you want to pass along with the navigation; for example, for logging or tracking purposes. This can be represented by any value type. The state for the navigation can subsequently be retrieved via the NavigationHistoryEntry.getState() method of the resulting history entry.
      • history
        • : An enumerated value that specifies how this redirect should be added to the navigation history. It can take one of the following values:
          • auto
            • : The default value, which lets the browser decide how to handle it:
              • If the original navigation occurred as a result of a Navigation.navigate() call, the value will be whatever was specified in the navigate() call's history option.
              • Otherwise, the value used is usually push, but it will become replace if the redirect points to the same URL as the pre-navigation URL.
          • push
            • : Adds a new NavigationHistoryEntry to the navigation history, and clears any available forward navigation (that is, if the user previously navigated to other locations, then used the back button to return through the history before initiating the navigation that caused the redirect).
          • replace
            • : Replaces the Navigation.currentEntry with the resulting new NavigationHistoryEntry.

[!NOTE] The redirect() method can convert the history behavior between auto, push, and replace, but it cannot turn a traverse navigation into a push/replace navigation and vice versa.

Return value

None (undefined).

Exceptions

  • InvalidStateError DOMException
    • : Thrown if:
      • The originating NavigateEvent was not intercepted.
      • The NavigateEvent.navigationType is not push or replace.
  • SyntaxError DOMException
    • : Thrown if the specified url is invalid.
  • SecurityError DOMException
    • : Thrown if the current document is unable to have its URL rewritten to the provided redirect url.

Examples

See the main NavigationPrecommitController page for an example.

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

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

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