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

`browsingContext.contextDestroyed` event

· 52CE 我爱测

The `browsingContext.contextDestroyed` event of the `browsingContext` module fires when a context is discarded from the browser, such as when a tab is closed or when an `<iframe>` is removed from the DOM.

The browsingContext.contextDestroyed event of the browsingContext module fires when a context is discarded from the browser, such as when a tab is closed or when an <iframe> is removed from the DOM.

Event data

The params field in the event notification is an object with the following fields:

  • children
    • : An array of context objects that represents child contexts. This event includes the full subtree of child contexts that were discarded (maxDepth is null). An empty array indicates that the context had no children.
  • clientWindow
    • : A string that contains the ID of the client window that contained this context.
  • context
    • : A string that contains the ID of the discarded context.
  • originalOpener
    • : A string that contains the ID of the context that originally opened this context. The value is null if the context was opened directly (not by another context).
  • parent
    • : A string that contains the ID of the parent context. The value is null if the context had no parent (that is, it was a top-level context). This field is present only for the top-level context in the event data.
  • url
    • : A string that contains the URL of the context, including the fragment, at the time it was discarded.
  • userContext
    • : A string that contains the ID of the user context associated with this context.

Description

If the subscription was scoped to specific contexts using the contexts parameter, the discarded context's ID is automatically removed from that subscription's scope after the event is emitted.

If the discarded context was the only context in the subscription's scope, the subscription itself is automatically removed.

Examples

Receiving an event when a tab is closed

Assume you have a WebDriver BiDi connection, an active session, and a subscription to browsingContext.contextDestroyed active.

When your automation script closes a tab using browsingContext.close, the browser sends the following notification:

{
  "type": "event",
  "method": "browsingContext.contextDestroyed",
  "params": {
    "context": "93ee5bd6-d256-4608-a002-9a8995cc0e5f",
    "children": [],
    "originalOpener": null,
    "url": "https://example.com/",
    "userContext": "default",
    "clientWindow": "08c697a1-2664-447d-9c88-52bcee3bb386",
    "parent": null
  }
}

Receiving an event when a tab with child frames is closed

Assume you have a WebDriver BiDi connection, an active session, and a subscription to browsingContext.contextDestroyed active.

Suppose a tab that contains two <iframe>s is closed. The browser sends the following notification, which includes the full subtree in the children field:

{
  "type": "event",
  "method": "browsingContext.contextDestroyed",
  "params": {
    "context": "93ee5bd6-d256-4608-a002-9a8995cc0e5f",
    "children": [
      {
        "context": "6442450945",
        "children": [],
        "originalOpener": null,
        "url": "https://example.com/frame1.html",
        "userContext": "default",
        "clientWindow": "08c697a1-2664-447d-9c88-52bcee3bb386"
      },
      {
        "context": "15032385537",
        "children": [],
        "originalOpener": null,
        "url": "https://example.com/frame2.html",
        "userContext": "default",
        "clientWindow": "08c697a1-2664-447d-9c88-52bcee3bb386"
      }
    ],
    "originalOpener": null,
    "url": "https://example.com/",
    "userContext": "default",
    "clientWindow": "08c697a1-2664-447d-9c88-52bcee3bb386",
    "parent": null
  }
}

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

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

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