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

`script.realmDestroyed` event

· 52CE 我爱测

The `script.realmDestroyed` event of the `script` module fires when the realm of a document, a worker, or a worklet is destroyed.

The script.realmDestroyed event of the script module fires when the realm of a document, a worker, or a worklet is destroyed.

Event data

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

  • realm
    • : A string that contains the ID of the realm that was destroyed.

Description

Together with script.realmCreated, use this event to monitor the lifetime of JavaScript realms.

This event fires when a document is unloaded, which happens when its context navigates to a new document or is closed. Unloading a document destroys the realm of the document and the realms of its worklets, so the event fires once for each of them. The event also fires when a worker reaches the end of its lifecycle or is terminated.

The event payload contains only the realm ID. If you need to identify the context or worker to which the realm belonged, match this ID against the realm information previously received from script.realmCreated or retrieved using script.getRealms.

After this event fires, the ID of the destroyed realm is no longer valid. The realm's objects are also released when it is destroyed, so you don't need to release them using script.disown.

Unlike script.realmCreated, this event is never replayed; it fires only for realms destroyed after you subscribe to it.

Examples

Tracking realms across a navigation

Assume you have a WebDriver BiDi connection and an active session with a subscription to both script.realmDestroyed and script.realmCreated.

Suppose a tab is open at https://example.com and you use browsingContext.navigate to navigate the context to https://example.org.

The browser first sends the following notification about the destroyed realm of the previous document:

{
  "type": "event",
  "method": "script.realmDestroyed",
  "params": {
    "realm": "7c37f4c0-abcd-1234-ef56-789012345678"
  }
}

It then sends the following notification about the realm of the new document:

{
  "type": "event",
  "method": "script.realmCreated",
  "params": {
    "context": "93ee5bd6-d256-4608-a002-9a8995cc0e5f",
    "origin": "https://example.org",
    "realm": "2b8e6d41-0f9a-4c3b-8d7e-1a2b3c4d5e6f",
    "type": "window"
  }
}

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

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

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