Share via

IFrame resources are freed when removed from the DOM

If you remove an iframe element from the Document Object Model (DOM), it no longer responds to DOM APIs calls.

This change was made in Internet Explorer 9; it affects IE9 standards mode (and later document modes).

When you remove an iframe element from the DOM of a webpage, the resources associated with the element (and its children) need to be freed. Memory needs to be reclaimed, window handles need to be closed, and so on.

In IE8 and earlier versions, this would happen during the next navigation event for the webpage (the parent document that first contained the iframe element).

Before the navigation event, code associated with the iframe element (or its contents) would continue to run. The iframe would continue using resources, even though it wasn't visible to the user. In rare cases, it was possible to negatively affect the user experience, especially when multiple iframe elements were removed from the DOM.

Starting with IE9, resources associated with iframe elements are freed as soon as the element is removed from the DOM (and any executing code completes) If you call DOM-related API on the iframe or any of its children, exceptions will trigger with the message "Can't execute code from a freed script."

If you need objects from the iframe, copy them out of the iframe before removing it from the DOM.