v156 · dom · feature removal

Deprecate and stop firing events during subframe removal

Remove a same-origin <iframe> from the DOM and Chrome has always run that frame’s pagehide, visibilitychange and unload handlers synchronously, inside your remove() call — handlers that can reach back into the parent’s half-mutated DOM. The HTML Standard’s “destroy a child navigable” never fires them. Behind a flag in Chrome 156 (and a Finch experiment from 162), Chrome stops firing them when the removed subframe can script an ancestor’s DOM. Navigation away from a page is untouched.

Behind a flag. To see the new behaviour, launch Chrome/Canary 156+ with --enable-blink-features=OmitSubframeDetachmentEventsOnRemoval (the same name works as a Finch feature: --enable-features=OmitSubframeDetachmentEventsOnRemoval). There is no chrome://flags entry, and because the runtime flag’s status is "test", --enable-experimental-web-platform-features does not turn it on. Every probe on these pages records what your browser actually does, so each one can report either outcome.

Stops firing (on DOM removal)

  • pagehide on the subframe’s window
  • visibilitychange on its document
  • unload on its window

…for that subframe and every same-agent descendant, via remove(), removeChild(), an ancestor’s removal, innerHTML/replaceChildren(), for <iframe>, <frame>, <object>, <embed>.

Still fires

  • All three events when the subframe navigates, or its ancestor navigates.
  • Removal of a subframe in another agent: cross-origin, data: URLs, sandbox without allow-same-origin.
  • beforeunload was never part of removal.

Never involved

  • moveBefore() — an atomic move never destroys the frame.
  • blur/focusout on removal — tracked separately as OmitBlurEventOnElementRemoval.

concepts

  1. Removal event recorder

    The canonical probe. Pick one of five ways to take a same-origin iframe out of the DOM, run it, and see exactly which of pagehide, visibilitychange and unload fired — and whether they ran synchronously inside the removal call while the iframe was still connected.

  2. Agent boundary matrix

    The gate, inspected. Seven real frame configurations — same-origin srcdoc, initial about:blank, nested, sandbox with and without allow-same-origin, a data: URL and the A1→B→A2 sandwich — each removed for real, with expected-before, expected-after and observed side by side.

  3. Navigation, moves & focus

    The edges. Navigating a frame still fires all three events (so pagehide is still the right navigation hook); moveBefore() reparents without any teardown while insertBefore() reloads; and focus events on removal belong to a different flag.

  4. Explicit teardown handshake

    The migration. An embedded draft-editor widget that saved its draft in pagehide loses work under the new behaviour. Compare it with three replacements that never depended on removal events: an explicit teardown message before removal, continuous state mirroring, and a parent-side MutationObserver watchdog.

  5. Re-entrancy hazard

    Why it shipped. A parent loops over a list of iframe cards removing each one; a card’s pagehide handler removes its neighbour mid-loop. Watch the interleaved trace show the child running inside the parent’s removal — and vanish when the events are omitted.

why it shipped

Firing script synchronously while a node is being removed is a long-running source of Blink bugs: the handler runs inside the parent’s mutation, can reach the parent’s DOM through parent/frameElement, and can remove, insert or navigate things the caller assumed were stable. The HTML Standard already dropped these events from “destroy a child navigable” for exactly that reason (whatwg/html#10194). Gecko and WebKit also historically fire them, so Chrome is aligning with the spec first, but narrowly: only subframes that share an agent with the document being mutated — the only ones whose handlers can synchronously re-enter it — lose the events. In the compat study (HTTP Archive October 2026 crawl plus a live review of 367 pages) that gate touched about 0.026% of mobile root pages, all benign cleanup, with no visible regressions; the broader “all subframes” option would have touched about 1.2%.

Developer takeaway: if an embedded same-origin frame needs to clean up when you remove it, tell it before you remove it, or have the parent own that cleanup. pagehide remains the right hook for the frame’s own navigations and for the top-level page going away.

the API surface

// Inside a same-origin subframe:
addEventListener("pagehide", flush);               // ✗ not on removal (156 flag)
document.addEventListener("visibilitychange", f);  // ✗ not on removal
addEventListener("unload", f);                     // ✗ not on removal

// In the parent:
frame.remove();            // no subframe script runs during this call any more
container.innerHTML = "";  // same
host.moveBefore(frame, null); // never fired anything (no teardown at all)

// Still fires all three:
frame.src = "/next.html";  // navigation of the subframe

Chromium implementation: ChildFrameDisconnector::ShouldFireUnloadEvents() in core/dom/child_frame_disconnector.cc, consulted by Document::DispatchUnloadEvents() only when no new document is committing in that frame. The gate is InSameAgent(subframe document, document being mutated).

references