At least spec tells that if an iframe is detached from the DOM and then becomes eligible for GC (not hold via JS reference not DOM connection) - it gets unloaded w/o onunload firing (4.8.2):<div><br></div><div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; background-color: transparent; font-family: Tinos; font-size: medium; ">
<span id="internal-source-marker_0.606545289978385" style="font-size: 11pt; font-family: Arial; color: rgb(0, 128, 0); background-color: transparent; font-weight: bold; font-style: italic; text-decoration: none; vertical-align: baseline; white-space: pre-wrap; ">On the other hand, if an </span><a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/the-iframe-element.html#the-iframe-element"><span style="font-size: 11pt; font-family: Verdana; color: rgb(0, 0, 153); background-color: transparent; font-weight: bold; font-style: italic; text-decoration: underline; vertical-align: baseline; white-space: pre-wrap; ">iframe</span></a><span style="font-size: 11pt; font-family: Arial; color: rgb(0, 128, 0); background-color: transparent; font-weight: bold; font-style: italic; text-decoration: none; vertical-align: baseline; white-space: pre-wrap; "> is </span><a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/infrastructure.html#remove-an-element-from-a-document"><span style="font-size: 11pt; font-family: Arial; color: rgb(0, 0, 204); background-color: transparent; font-weight: bold; font-style: italic; text-decoration: underline; vertical-align: baseline; white-space: pre-wrap; ">removed</span></a><span style="font-size: 11pt; font-family: Arial; color: rgb(0, 128, 0); background-color: transparent; font-weight: bold; font-style: italic; text-decoration: none; vertical-align: baseline; white-space: pre-wrap; "> from a </span><a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/infrastructure.html#document"><span style="font-size: 11pt; font-family: Verdana; color: rgb(0, 0, 153); background-color: transparent; font-weight: bold; font-style: italic; text-decoration: underline; vertical-align: baseline; white-space: pre-wrap; ">Document</span></a><span style="font-size: 11pt; font-family: Arial; color: rgb(0, 128, 0); background-color: transparent; font-weight: bold; font-style: italic; text-decoration: none; vertical-align: baseline; white-space: pre-wrap; "> and is then subsequently garbage collected, this will likely mean (in the absence of other references) that the </span><a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/browsers.html#child-browsing-context"><span style="font-size: 11pt; font-family: Arial; color: rgb(0, 0, 204); background-color: transparent; font-weight: bold; font-style: italic; text-decoration: underline; vertical-align: baseline; white-space: pre-wrap; ">child browsing context</span></a><span style="font-size: 11pt; font-family: Arial; color: rgb(0, 128, 0); background-color: transparent; font-weight: bold; font-style: italic; text-decoration: none; vertical-align: baseline; white-space: pre-wrap; ">'s </span><a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/browsers.html#windowproxy"><span style="font-size: 11pt; font-family: Verdana; color: rgb(0, 0, 153); background-color: transparent; font-weight: bold; font-style: italic; text-decoration: underline; vertical-align: baseline; white-space: pre-wrap; ">WindowProxy</span></a><span style="font-size: 11pt; font-family: Arial; color: rgb(0, 128, 0); background-color: transparent; font-weight: bold; font-style: italic; text-decoration: none; vertical-align: baseline; white-space: pre-wrap; "> object will become eligble for garbage collection, which will then lead to that</span><a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/browsers.html#browsing-context"><span style="font-size: 11pt; font-family: Arial; color: rgb(0, 0, 204); background-color: transparent; font-weight: bold; font-style: italic; text-decoration: underline; vertical-align: baseline; white-space: pre-wrap; ">browsing context</span></a><span style="font-size: 11pt; font-family: Arial; color: rgb(0, 128, 0); background-color: transparent; font-weight: bold; font-style: italic; text-decoration: none; vertical-align: baseline; white-space: pre-wrap; "> being </span><a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/browsers.html#a-browsing-context-is-discarded"><span style="font-size: 11pt; font-family: Arial; color: rgb(0, 0, 204); background-color: transparent; font-weight: bold; font-style: italic; text-decoration: underline; vertical-align: baseline; white-space: pre-wrap; ">discarded</span></a><span style="font-size: 11pt; font-family: Arial; color: rgb(0, 128, 0); background-color: transparent; font-weight: bold; font-style: italic; text-decoration: none; vertical-align: baseline; white-space: pre-wrap; ">, which will then lead to its </span><a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/infrastructure.html#document"><span style="font-size: 11pt; font-family: Verdana; color: rgb(0, 0, 153); background-color: transparent; font-weight: bold; font-style: italic; text-decoration: underline; vertical-align: baseline; white-space: pre-wrap; ">Document</span></a><span style="font-size: 11pt; font-family: Arial; color: rgb(0, 128, 0); background-color: transparent; font-weight: bold; font-style: italic; text-decoration: none; vertical-align: baseline; white-space: pre-wrap; "> being </span><a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/browsers.html#discard-a-document"><span style="font-size: 11pt; font-family: Arial; color: rgb(0, 0, 204); background-color: transparent; font-weight: bold; font-style: italic; text-decoration: underline; vertical-align: baseline; white-space: pre-wrap; ">discarded</span></a><span style="font-size: 11pt; font-family: Arial; color: rgb(0, 128, 0); background-color: transparent; font-weight: bold; font-style: italic; text-decoration: none; vertical-align: baseline; white-space: pre-wrap; "> also. This happens without notice to any scripts running in that </span><a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/infrastructure.html#document"><span style="font-size: 11pt; font-family: Verdana; color: rgb(0, 0, 153); background-color: transparent; font-weight: bold; font-style: italic; text-decoration: underline; vertical-align: baseline; white-space: pre-wrap; ">Document</span></a><span style="font-size: 11pt; font-family: Arial; color: rgb(0, 128, 0); background-color: transparent; font-weight: bold; font-style: italic; text-decoration: none; vertical-align: baseline; white-space: pre-wrap; ">; for example, no </span><span style="font-size: 11pt; font-family: Verdana; color: rgb(255, 69, 0); background-color: transparent; font-weight: bold; font-style: italic; text-decoration: none; vertical-align: baseline; white-space: pre-wrap; ">unload</span><span style="font-size: 11pt; font-family: Arial; color: rgb(0, 128, 0); background-color: transparent; font-weight: bold; font-style: italic; text-decoration: none; vertical-align: baseline; white-space: pre-wrap; "> events are fired (the "</span><a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/history.html#unload-a-document"><span style="font-size: 11pt; font-family: Arial; color: rgb(0, 0, 204); background-color: transparent; font-weight: bold; font-style: italic; text-decoration: underline; vertical-align: baseline; white-space: pre-wrap; ">unload a document</span></a><span style="font-size: 11pt; font-family: Arial; color: rgb(0, 128, 0); background-color: transparent; font-weight: bold; font-style: italic; text-decoration: none; vertical-align: baseline; white-space: pre-wrap; ">" steps are not run).</span></div>
<div><br></div><div>Seems there are bugs to fix :-) </div><div><br></div><br><div class="gmail_quote">On Tue, Aug 24, 2010 at 3:42 PM, Dirk Pranke <span dir="ltr"><<a href="mailto:dpranke@chromium.org">dpranke@chromium.org</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">On a related note, the behavior of onUnload in this situation is quite<br>
unclear. Should onUnload() fire if an iframe is detached from the DOM?<br>
<br>
The following test illustrates this, and behaves differently in<br>
webkit, opera, FF, and IE (all of which are different from the spec<br>
:).<br>
<br>
<a href="http://nfs.oldkentuckyshark.com/tests/detached_iframes/" target="_blank">http://nfs.oldkentuckyshark.com/tests/detached_iframes/</a><br>
<font color="#888888"><br>
-- Dirk<br>
</font><div><div></div><div class="h5"><br>
On Tue, Aug 24, 2010 at 3:27 PM, Dmitry Titov <<a href="mailto:dimich@chromium.org">dimich@chromium.org</a>> wrote:<br>
> Indeed, in WebKit you normally see #1 (iframe unloads). We have added the<br>
> ability to move 'live' iframe, as Adam mentions, potentially across<br>
> documents, while keeping it completely alive, with XHRs loading, events<br>
> firing etc (aka 'magic iframe' feature). One would need to use adoptNode()<br>
> API to do that, something like:<br>
> var iframe = document.getElementById("test");<br>
> other_document.adoptNode(iframe);<br>
> other_document.getElementById("newParent").attachChild(iframe);<br>
> WebKit has a bug (<a href="https://bugs.webkit.org/show_bug.cgi?id=13574" target="_blank">https://bugs.webkit.org/show_bug.cgi?id=13574</a>) to enable<br>
> moving iframes w/o reloading. FF has a bug on that as well<br>
> (<a href="https://bugzilla.mozilla.org/show_bug.cgi?id=254144" target="_blank">https://bugzilla.mozilla.org/show_bug.cgi?id=254144</a>) but it's hard to say<br>
> when exactly those will be fixed. I hope to fix WebKit issue at some point.<br>
> While discussing 'magic iframe', Ian Hickson pointed out that nothing in the<br>
> spec actually mandates discarding the live document inside iframe simply<br>
> because it's iframe element is connected/disconnected to DOM of the parent<br>
> document. Here is a note from the HTML5 spec about that:<br>
> Removing an iframe from a Document does not cause its browsing context to be<br>
> discarded. Indeed, an iframe's browsing context can survive its original<br>
> parent Document if its iframe is moved to another Document.<br>
><br>
> So it seems the right behavior is to keep the content alive. It's not clear<br>
> why the events would not fire during DOM operations though. Perhaps they<br>
> should, since nothing is changing from the perspective of the document<br>
> loaded into iframe - for example, XHR probably should continue loading<br>
> content if it was doing so before iframe was disconnected from its parent<br>
> node. Doing some suspension (as for example is done when a page goes into<br>
> back-forward cache?) would be way more complex mechanism to have, with<br>
> necessary events on pause/unpause so the live document could re-start async<br>
> operations correctly.<br>
> Dmitry<br>
> On Tue, Aug 24, 2010 at 1:38 PM, Adam Barth <<a href="mailto:w3c@adambarth.com">w3c@adambarth.com</a>> wrote:<br>
>><br>
>> This seems related to the "magic iframe" concept that was recently<br>
>> added in WebKit.  Basically, magic iframe lets you move an iframe from<br>
>> one document to another without blowing away the JavaScript/DOM state<br>
>> of the iframe.  The way this works is that the iframe remains "alive"<br>
>> until the browser returns to the main event loop.  If a living iframe<br>
>> gets added to a document, then it keeps all it's state.  This feature<br>
>> is useful for sites like Gmail that have chat windows that can be<br>
>> opened from the main document.  If the user closes the main document,<br>
>> the chat windows can adopt some iframe that keeps the proper state.<br>
>><br>
>> Adam<br>
>><br>
>><br>
>> On Tue, Aug 24, 2010 at 1:30 PM, Ben Lerner <<a href="mailto:blerner@cs.washington.edu">blerner@cs.washington.edu</a>><br>
>> wrote:<br>
>> >  There seems to be a bit of disagreement among browsers about how event<br>
>> > loops and iframes interact when an iframe is removed and then reinserted<br>
>> > into its parent document.  Consider the following two documents: the<br>
>> > parent<br>
>> > document has a button that removes or reattaches an iframe to the<br>
>> > document,<br>
>> > while the second simply sets an interval to update the page content.<br>
>> ><br>
>> > Page1.html:<br>
>> > <!DOCTYPE HTML><br>
>> > <html><br>
>> > <body><br>
>> > <p><button onclick="toggleInDoc();">Show/hide</button></p><br>
>> > <iframe id="test" src="page2.html"></iframe><br>
>> > <script><br>
>> >    var test = document.getElementById("test");<br>
>> >    function toggleInDoc() {<br>
>> >      if (test.parentNode == null)<br>
>> >        document.body.appendChild(test);<br>
>> >      else<br>
>> >        document.body.removeChild(test);<br>
>> >    }<br>
>> > </script><br>
>> > </body><br>
>> > </html><br>
>> ><br>
>> ><br>
>> > Page2.html:<br>
>> > <!DOCTYPE HTML><br>
>> > <html><br>
>> > <body><br>
>> > <p id="test"></p><br>
>> > <script><br>
>> >    window.setInterval(function() {<br>
>> > document.getElementById("test").innerHTML<br>
>> > += "."; }, 500);<br>
>> > </script><br>
>> > </body><br>
>> > </html><br>
>> ><br>
>> ><br>
>> > Assume the user waits until the interval has fired several times, then<br>
>> > presses the button, waits a while, and presses it again.  There are<br>
>> > three<br>
>> > possible outcomes:<br>
>> > 1. When the iframe is reattached, the inner page reloads.  This seems to<br>
>> > go<br>
>> > beyond the wording of the spec, which says only "When an iframe element<br>
>> > is<br>
>> > first inserted into a document, the user agent must create a nested<br>
>> > browsing<br>
>> > context, and then process the iframe attributes for the first time."<br>
>> >  (This<br>
>> > isn't the first time the iframe is inserted into the document, so we<br>
>> > shouldn't process the iframe attributes again.)<br>
>> ><br>
>> > 2. The interval (and presumably, all events) in the iframe is paused<br>
>> > while<br>
>> > it's been detached (since the document is no longer fully active, but it<br>
>> > also has not been discarded because of the global reference to its<br>
>> > container<br>
>> > element).<br>
>> ><br>
>> > 3. The interval (and presumably, all events) continues to fire while<br>
>> > it's<br>
>> > been detached, and the content of page2 will have changed while it's<br>
>> > been<br>
>> > detached from page1.<br>
>> ><br>
>> > So far, Chrome 6, Opera 10.6 and Firefox 3.6 follow #1, and IE 8 follows<br>
>> > #3.<br>
>> >  My reading of the "fully active" clause of the spec leads me to expect<br>
>> > #2.<br>
>> >  Which of these behaviors is the desired one?  And/or, would it be<br>
>> > desirable<br>
>> > to permit authors to specify which behavior they intend?<br>
>> ><br>
>> > Thanks,<br>
>> > ~ben<br>
>> ><br>
><br>
><br>
</div></div></blockquote></div><br></div>