Load event for iFrame not fired in IE

32,399

Solution 1

I think for iframes in Internet Explorer you can't set that event handler (onload) programmatically, you need to specify it in your markup.

Something like:

<iframe id="myFrame" onload="myFunction();"></iframe>

Otherwise IE is just going to ignore the function.

Solution 2

IE might have already loaded the content (and fired the event) before you add the handler. I found that when I statically specified the iframe src attr, and added $(x).load event handlers via jquery, firefox (3.6.28) triggered my handlers but IE (8.0.6001.18702) didn't.

I ended up adjusting my test program so that it set the iframe src via javascript after adding the $(x).load handler. My $(x).load handler was called at the same points in IE and Firefox (but note a handler added via iframe onload attribute behaved differently between IE and FF) . Here is what I ended up with:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=Edge">
<script type="text/javascript" src="jquery-ui/js/jquery-1.6.2.min.js"></script>

<script language="javascript">

function show_body(name, $iframe) {
  $('.log').append(name+': '+$iframe.contents().find('body').html()+'<br/>');
}

function actuallyLoaded(name, x) {
  $('.log').append(name+' actually loaded<br/>');
}

$(document).ready(function(){
  $('.i1').load(function(){show_body('i1', $('.i1'));});
  $('.i1').attr('src', 'eb_mce_iframe_content.html');
  var $x=$('.i1').clone().removeClass('i1');
  $('body').append($x);
  $x.load(function(){show_body('x', $x);});
  $x.attr('src', 'eb_mce_iframe_content.html');
});

</script>

</head>
<body>

<iframe class="i1" onload="actuallyLoaded($(this).attr('class')+'/'+$(this).attr('src'), this);">
</iframe>

<div class="log">
</div>

</body>
</html>

... and here was the Firefox "log":

i1/eb_mce_iframe_content.html actually loaded i1:

Fred the fox.

/eb_mce_iframe_content.html actually loaded x:

Fred the fox.

Solution 3

Assigning the handler directly to onload works in Chrome, FF, and IE (tested with IE 8).

(function (selector) {
    var frame = $(selector).get(0);

    if (frame) {
        frame.onload = function () {
            alert('frame loaded.');
        };
    }
})('#myframe');

Solution 4

Using the JavaScript code with jQuery from here works if you change the if ($.browser.safari || $.browser.opera) { line to if ($.browser.safari || $.browser.opera || $.browser.msie) {. So you have the following:

$(function(){

    var iFrames = $('iframe');

    function iResize() {

        for (var i = 0, j = iFrames.length; i < j; i++) {
          iFrames[i].style.height = iFrames[i].contentWindow.document.body.offsetHeight + 'px';}
        }

        if ($.browser.safari || $.browser.opera || $.browser.msie) { 

           iFrames.load(function(){
               setTimeout(iResize, 0);
           });

           for (var i = 0, j = iFrames.length; i < j; i++) {
                var iSource = iFrames[i].src;
                iFrames[i].src = '';
                iFrames[i].src = iSource;
           }

        } else {
           iFrames.load(function() { 
               this.style.height = this.contentWindow.document.body.offsetHeight + 'px';
           });
        }

    });

Solution 5

I uses readystatechange event for IE.

var $iframe = $("<iframe>");
$iframe.on("load readystatechange", callback);
Share:
32,399
Muleskinner
Author by

Muleskinner

Danish Windows (vb.net) and web developer (asp.net, MySQL, javaScript) living in Granada, Spain. Working for danish HRM&amp;D company. Latest work: http://jobmatchprofile.com http://garudabp.com/?l=enu http://www.egaruda.com

Updated on December 11, 2020

Comments

  • Muleskinner
    Muleskinner over 3 years

    Why is the load event not fired in IE for iFrames?

    Please take a look at this example.

    Work perfectly as expected in FF and Chrome, but IE fails.