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

`browsingContext.close` command

· 52CE 我爱测

The `browsingContext.close` command of the `browsingContext` module closes the specified top-level context.

The browsingContext.close command of the browsingContext module closes the specified top-level context.

Syntax

/* With required parameters */
{
  "method": "browsingContext.close",
  "params": {
    "context": "93ee5bd6-d256-4608-a002-9a8995cc0e5f"
  }
}

/* With required and optional parameters */
{
  "method": "browsingContext.close",
  "params": {
    "context": "93ee5bd6-d256-4608-a002-9a8995cc0e5f",
    "promptUnload": true
  }
}

Parameters

The params field contains:

  • context
  • promptUnload
    • : A boolean that indicates whether the browser runs beforeunload event handlers before closing the context.
      • false: The specified context closes immediately without running beforeunload event handlers. This is the default.
      • true: The browser runs beforeunload event handlers before closing the specified context. Any resulting prompt is handled as defined by the unhandledPromptBehavior capability specified via the session.new command.

Return value

The result field in the response is an empty object ({}).

Errors

  • invalid argument
    • : A required parameter is missing or has an invalid type. This error is also returned when the context specified by context is not a top-level context.
  • no such frame
    • : No context with the given context ID is found.

Examples

Closing a tab with a page unload prompt

The following example shows how to close a tab and allow its beforeunload event handlers to run before closing.

With a WebDriver BiDi connection, suppose a session is created via session.new with the default field of the unhandledPromptBehavior capability set to "accept" so that the browser accepts any confirmation prompt automatically. First get the context ID using browsingContext.getTree, then send the following message:

{
  "id": 1,
  "method": "browsingContext.close",
  "params": {
    "context": "5e5e96e8-5247-4f22-9b35-a4a2d841cbaa",
    "promptUnload": true
  }
}

The browser closes the context and responds as follows:

{
  "id": 1,
  "type": "success",
  "result": {}
}

Since promptUnload is true, the browser runs any beforeunload handlers on the page before closing. The confirmation prompt, if shown, is automatically accepted based on the unhandledPromptBehavior setting defined in session.new.

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

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

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