 <p>Update: the Firefox bug is now marked as “fixed” and will make it into a near-future update.</p> <hr> <p>I’ve run into this too and would <strong>love</strong> to see the root cause fixed.</p> <ul> <li><p>I talked to a couple of Firefox developers (mbrubeck and gavin), and they think that it’s a bug! <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=300849" rel="nofollow">The same issue was reported, and fixed,</a> in 2005 for Firefox 1.9. Then, <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=489259" rel="nofollow">bug 489259</a> was opened in 2009. mbrubeck has graciously moved it out of the “unconfirmed” pile.</p></li> <li><p>Safari behaves better than Firefox, but an error message (“One error in opening the page…”) shows up in the status bar if you remove the iframe during the <code>load</code> event. I found two similar WebKit bugs which have been open since 2007: <a href="https://bugs.webkit.org/show_bug.cgi?id=15485" rel="nofollow">15485</a> and <a href="https://bugs.webkit.org/show_bug.cgi?id=13281" rel="nofollow">13281</a>. </p></li> </ul> <p>This appears to happen when you remove an <code>iframe</code> from the document during the <code>load</code> event. JavaScript events fire <em>synchronously</em> — that is, in series with the browser’s own handling of the web page. That’s why it's possible to prevent a form from being submitted or prevent a keypress or mouse click from being registered from within an event handler.</p> <p>The last time this bug was fixed in Firefox, the cause was that the removing an <code>iframe</code> from the page makes it forget which page owned it, but the <code>iframe</code> notifies the page that it was finished loading <em>after</em> the <code>load</code> event.</p> <p>Anything you schedule with <code>setTimeout</code> happens after the current cycle of the event loop — after the browser finishes what it’s doing <em>right now</em>. So, using setTimeout to remove the <code>iframe</code>, even with a timeout of zero, lets it finish:</p> <pre><code>iframe.onload = function(){ // Do work with the content of the iframe… setTimeout(function(){ iframe.parentNode.removeChild(iframe); }, 0); } </code></pre> <p>You can find this technique in use <a href="https://github.com/malsup/form/blob/e77e287c8024d200909a7b4f4a1224503814e660/jquery.form.js#L511" rel="nofollow">in the jQuery form plugin</a>.</p>