Window object properties are cleared when orphaned
Properties on the global object (window) are cleared when a window is orphaned. The properties are cleared to allow garbage collection of the orphaned window when no additional references to it are found. Additionally, timers stop firing and event propagation (inside the orphaned window) stops immediately. The following definitions will help in understanding this topic:
Orphaned window: When a transient iFrame is removed from the DOM tree, its window is considered "orphaned" (alive, but not directly reachable) until it is closed.
When iFrame elements are removed from the DOM tree, several things happen:
- Their orphaned window object is "cleaned" to allow the orphaned window to garbage collection. A cleaned window object (that is, global object) will not have any properties visible on it. The cleaning only involves property removal on the window object—it does not actually delete the objects formerly referenced by those properties. This provides the ability for any built-in or user-defined objects present on the orphaned window to be reachable if, and only if, there is another external reference to the object.
- Existing timers (for example, setTimeout) are canceled and no further timers are allowed to run
- Event propagation that was in progress within the window that becomes orphaned is stopped (for example, as if the stopImmediatePropagation API was invoked on the related event)
In previous versions of Windows Internet Explorer (including legacy Windows Internet Explorer 9 document modes), when an iFrame was removed from the DOM tree, the memory for the orphaned window was reclaimed only at page navigation time. Prior to page navigation, the orphaned window's memory would persist without being garbage collected. In rare cases when sites use multiple transient iFrames to download and/or run content without intervening page navigations, the amount of memory per orphaned window would compound until it negatively impacted a user's experience on the page. Pre-existing references to objects within the orphaned window remain available; however, re-requesting these properties via the global object will fail. Failures will manifest as script errors where "myRequestedProperty" does not exist or is undefined.
We recommend that:
- Objects required from an orphaned window have pre-existing references (from outside the orphaned window) in order to be reachable, and that functions executed in the context of the orphaned window avoid references to properties from the global object (for example, Array, Object)
- Code that expects timers to fire should be made resilient to the possibility of their window becoming orphaned, resulting in the cancellation of existing timers
- Event handlers that have a side effect of removing their containing iFrame should be made resilient to the possibility of their window becoming orphaned, resulting in a stopImmediatePropagation of the currently propagating event