tags:

views:

252

answers:

1

Hi,

I have an iframe in my HTML, and I want to be notified when it's fully loaded. I have this testcase:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Test</title>

<script language="javascript" type="text/javascript">
function addListener(el, type, func, capture) {
 if (el.addEventListener){
  el.addEventListener(type, func, capture); 
 } else if (el.attachEvent){
  el.attachEvent('on'+type, func);
 }
}
function addhandlers(frame) {
 addListener(frame, "load", function(e) { onDomLoaded(e); }, true);
}
function onDomLoaded(e) {
 alert("loaded");
}
</script>

</head>
<body>
<form action="#">
<input type="button" value="Add handlers" onclick="addhandlers(window.frames[0]);" />
</form>

<iframe id="frm" height="300" src="test.htm" width="700"></iframe>

</body>
</html>

The test.htm file is just a standard html file with a few regular links to places.

First I click the Add Handlers button to add the listeners, then I click any link in test.htm inside the iframe. My expectation is that I get an alert, but nothing happens. Why, and how do I solve it? I'm currently looking for a solution that works in firefox and IE (but nothing happens in either browser).

A: 

A workaround would be to include javascript within test.htm

test.htm:

function addListener(el, type, func, capture) {
    if (el.addEventListener){
            el.addEventListener(type, func, capture); 
    } else if (el.attachEvent){
            el.attachEvent('on'+type, func);
    }
}
function addhandlers(frame) {
        addListener(frame, "load", function(e) { onDomLoaded(e); }, true);
}

function onDomLoaded(e) {  
   window.parent.iframeLoaded()
}

parentpage.htm:

function iframeLoaded() {
    alert('loaded');
}

Note that this only works if both files are on the same domain

Paul Wagener
unfortunately I cannot change the test.htm page in the real setting, as it may be on another domain.
Tominator