0

I'm trying fade in a div after an iframe has loaded. The iframe is being called after the window has loaded.

I can load the iframe fine but I can't figure out how to make the div fadeIn after the load has completed. Thanks ahead for your assistance.

This is what I've tried but the load function following the html isn't happening

$(function() {
    $(window).load( function() {
        $("#ad").html("<iframe ... </iframe>").load(function(){
            $("#ad").fadeIn("normal");
        });
    });
});
xiatica
  • 1,546
  • 2
  • 20
  • 24

3 Answers3

0

Javascript callback when IFRAME is finished loading?

Community
  • 1
  • 1
Sjoerd
  • 74,049
  • 16
  • 131
  • 175
0

Set your #ad to display:none; by default. Then try the following.

$(function() {
    $(window).load( function() {
        $("#ad").html("<iframe ... </iframe>").fadeIn("normal");       
    });
});
Hussein
  • 42,480
  • 25
  • 113
  • 143
0

You can get a handle on the iframe's window object and then check document.readyState to see if the load event has already fired. If it has, it will equal "complete".

Something like this:

$('<iframe>').attr('src', '/pow.html').appendTo(document.body);
var win = window[window.length - 1];

var fn = function () { console.log('loaded!') };
$(win).load(fn);
if (win.document.readyState === "complete") fn();
substack
  • 3,346
  • 24
  • 14