Reliably detect when an iframe has loaded?

P

Peter Michaux

I have a form which posts to a hidden iframe. The iframe is the form's
target. This is because the form must initiate a file download. So the
form is in the parent window and the iframe is the child.

There is potential that the server sends an error response to the
child window. I'm being asked to detect that error response from the
parent window so that the server-side programmers do not have to
change what they send as the response.

My suggestion was to change the server's response and have the server
send back a page with a snip of JavaScript to cause action in the
parent window. Something like "window.parent.location="/login/page"
for a session timeout. As far as I know, having the server send back
some JavaScript is the only standard way to have the parent document
take action based on an error loading in the iframe. This has been
judged too onerous a change to the server-side programming by the
server-side programmers.

So in case I don't know about something I could be using in this
situation, is there a standard, reliable way to detect that the iframe
has loaded its contents? If so I could then parse the iframe's
contents to detect an error.

Thanks,
Peter
 
P

Peter Michaux

This seems to work... :

(function isLoaded() {
  if (iframe.contentWindow && iframe.contentWindow.document &&
iframe.contentWindow.document.body &&
iframe.contentWindow.document.body.innerHTML) {
    alert("ONLOAD: "+ iframe.contentWindow.document.body.innerHTML);
  } else {
    setTimeout(isLoaded, 333);
  }

})();

Thanks for the suggestion. I actually did think about polling but I
was thinking about polling for another property only introduced in
IE8. It seems contentWindow is all the way back to IE5.5. Polling does
make me worry, however, although that may be unjustified worrying.

I think the problem with polling for my situation is that if the
request is successful, that is a file is downloaded, the iframes
document body is not affected in a predictable way. So I don't think
there would be a predictable way to stop the polling. Perhaps that
wouldn't matter.

Even though the "load" event of an iframe is non-standard, it does
seem to be a de facto standard. I see that other libraries like YUI
depend upon the load event and so it is unlikely future browsers will
be able to not have that event. I think I'm going to go with the
iframe "load" event for now.

Peter
 
P

Peter Michaux

Hmm, first I wrote:

iframe.onload= function () {
  alert("onload event handler");

};

and it didn't work in IE6 nor 7. Instead, iframe.attachEvent does :

Yes, I was using attachEvent in IE also and it worked for me. I didn't
try the onload property.

It seems to me the lack of the load event for iframes may have been a
specification bug. I don't know why they would include the load event
for frames in a frameset but exclude on purpose for an iframe.

Peter
 
A

Andrew Poulos

Jorge said:
Hmm, first I wrote:

iframe.onload= function () {
alert("onload event handler");
};

and it didn't work in IE6 nor 7. Instead, iframe.attachEvent does :

For IE I've been using, and never thought to try attachEvent:

var doc = document.getElementById("myFrame");
doc.onreadystatechange = function() {
if (doc.readyState == "complete") {
// blah
}
};

Andrew Poulos
 
G

Gabriel Gilini

Yes, I was using attachEvent in IE also and it worked for me. I didn't
try the onload property.

It seems to me the lack of the load event for iframes may have been a
specification bug. I don't know why they would include the load event
for frames in a frameset but exclude on purpose for an iframe.

Regardless of being present in most user agents, since it's not on the
standards, I'd mix both approaches to make sure that it works
everywhere.
 
M

Michael J. Ryan

I have a form which posts to a hidden iframe. The iframe is the form's
target. This is because the form must initiate a file download. So the
form is in the parent window and the iframe is the child.

There is potential that the server sends an error response to the
child window. I'm being asked to detect that error response from the
parent window so that the server-side programmers do not have to
change what they send as the response.

My suggestion was to change the server's response and have the server
send back a page with a snip of JavaScript to cause action in the
parent window. Something like "window.parent.location="/login/page"
for a session timeout. As far as I know, having the server send back
some JavaScript is the only standard way to have the parent document
take action based on an error loading in the iframe. This has been
judged too onerous a change to the server-side programming by the
server-side programmers.

So in case I don't know about something I could be using in this
situation, is there a standard, reliable way to detect that the iframe
has loaded its contents? If so I could then parse the iframe's
contents to detect an error.

untested...


var t = document.getElemmentById("iframeId");

///before submit...
t.documentElement.clear();
t.documentElement.write("OK");
t.documentElement.close();

//submit form...
....

//check in 5 seconds...
window.setTimeout(function(){
if (t.documentElement.innerHTML != "OK") {
...handle error...
}
}, 5000);
 
M

Michael J. Ryan

doh! (iframe.contentWindow && iframe.contentWindow.document)
not iframe.documentElement
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top