plasticwrapper

design and development for the web


Internet Explorer iframe.onload support

I have been trying to trigger javascript after an iframe is loaded with foreign content.  Everything I read told me that the following code should work in all browsers (but, it doesn't):

var iframe = document.createElement("iframe");
iframe.onload = function () { alert("iframe is done loading."); };
document.body.appendChild(iframe);

Internet Explorer seemed to be clobbering the containing window's onload event when I would set iframe.onload in this way.  I set out to find a different way to make this work.

Many Google Searches Later...

Thankfully, I found this in-depth post by Nicholas C. Zakas that lead me to a very helpful code snippet:

var iframe = document.createElement("iframe");
iframe.src = "simpleinner.htm";

if (iframe.attachEvent){
    iframe.attachEvent("onload", function(){
        alert("iframe is now loaded.");
    });
} else {
    iframe.onload = function(){
        alert("iframe is now loaded.");
    };
}

document.body.appendChild(iframe);

So, Internet Explorer really does support iframe.onload, but it's hidden away. It will only work if you define onload using attachEvent.

Tags: , , , , , , , ,

Categories

Recommended Materials