views:

826

answers:

2

I know that onload event waits for page resources to load before firing - images, stylesheets, etc.

But does this include IFrames inside the page? In other words, is it guaranteed that all the child Frames' onloads will always fire before the parent's does?

Also, please let me know if behavior varies between browsers.

+2  A: 

As I see on my pages, each iframe got independent onload, and top-frame onload doesn't wait for iframes to fire.

I got gif/png banners on my site that sometimes loads very slowly, so I put them into iframe and that made whole site and onload event to work faster.

Thinker
+3  A: 

No, it doesn't. If you want to do something like that, you'll need to add an onload handler for the iframe. You can do this nicely with jQuery:

  <iframe src="http://digg.com"&gt;&lt;/iframe&gt;
  <script>
    var count = $('iframe').length;
    $(function() {
      // alert('loaded'); // will show you when the regular body loads
      $('iframe').load(function() {
        count--;
        if (count == 0)
            alert('all frames loaded');
      });
    });
  </script>

This would alert when all the frames are loaded.

See the example:

http://jsbin.com/azilo

altCognito