I serve ads through an iFrame. The ad network’s servers are much slower than mine, so I asyncronously load the iFrame on the window.onload event.
// (using Prototype library)
Event.observe(window, 'load', function() {
$('ad').writeAttribute('src', '/ad.html');
// other initialization here
});
A problem occurs when you enter the site via the browser’s back button. Unexpectedly, the ad iFrame attempts to load immediately (before the load listener above sets the ‘src’ attribute), delaying the load event for a few seconds. During these few seconds, the site is unusable because I do a bunch of initialization in window.onload.
As far as I know, this only happens in Firefox. How do I prevent this blocking load?
Theory
The issue you’ve noticed could be described like this: an iframe in a document loads the URL loaded in it at the moment the outer page was navigated from, not the URL specified in the outer page’s HTML source (or an empty page in your case).
The fact that it delays
onloadis just a corollary: the load event is delayed until all the resources (images, frames, etc) are finished loading.I couldn’t find documentation explaining that it should happen this way, nor any bug reports acknowledging it as a known issue, but FWIW I see exactly the same behavior with Google Chrome 10.0.612.3 on the testcase below.
This behavior, even though unintuitive in your case, has some logic behind it: document loads in frames historically are recorded in the session history (back/forward navigation history). Suppose you open a site, that has its main content in a frame, and navigate the inner iframe. After going away from the site, then returning via the back button, the browser should arguably load the last loaded page in the frame.
Solutions
Surprisingly, this doesn’t appear to be a common question, so these suggestions are based on my understanding of the issue, I haven’t tested them in any scenario more real than the testcase below.
If you pulled this off though, there would be no
loadevent at all, the page would just be restored exactly in the state it was left in, and it would much snappier than anything that hits the network.loadevent not be blocked by the iframe. For this you could use the DOMContentLoaded event.Appendix: Testcase