Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Reliably detect when an iframe has loaded?

Reply
Thread Tools

Reliably detect when an iframe has loaded?

 
 
Peter Michaux
Guest
Posts: n/a
 
      06-16-2009
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
 
Reply With Quote
 
 
 
 
Peter Michaux
Guest
Posts: n/a
 
      06-16-2009
On Jun 16, 10:08*am, Peter Michaux <(E-Mail Removed)> wrote:

> 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?


The "load" event on an iframe doesn't seem to be standard.

http://www.w3.org/TR/DOM-Level-2-Eve...ngs-htmlevents

Peter

 
Reply With Quote
 
 
 
 
Jorge
Guest
Posts: n/a
 
      06-16-2009
On Jun 16, 7:28*pm, Peter Michaux <(E-Mail Removed)> wrote:
> On Jun 16, 10:08*am, Peter Michaux <(E-Mail Removed)> wrote:
>
> > 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?

>
> The "load" event on an iframe doesn't seem to be standard.
>
> http://www.w3.org/TR/DOM-Level-2-Eve...nts-eventgroup...
>
> Peter


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);
}
})();

--
Jorge.
 
Reply With Quote
 
Jorge
Guest
Posts: n/a
 
      06-16-2009
On Jun 16, 8:13*pm, Jorge <(E-Mail Removed)> wrote:
>
> This seems to work... :
> (...)


.... for iframes coming from the same domain...

http://jorgechamorro.com/cljs/063/

--
Jorge.
 
Reply With Quote
 
Peter Michaux
Guest
Posts: n/a
 
      06-16-2009
On Jun 16, 11:13*am, Jorge <(E-Mail Removed)> wrote:
> On Jun 16, 7:28*pm, Peter Michaux <(E-Mail Removed)> wrote:
>
> > On Jun 16, 10:08*am, Peter Michaux <(E-Mail Removed)> wrote:

>
> > > 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?

>
> > The "load" event on an iframe doesn't seem to be standard.

>
> >http://www.w3.org/TR/DOM-Level-2-Eve...nts-eventgroup...

>
> > Peter

>
> 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
 
Reply With Quote
 
Jorge
Guest
Posts: n/a
 
      06-16-2009
On Jun 16, 9:17*pm, Peter Michaux <(E-Mail Removed)> wrote:
> (...)
> Even though the "load" event of an iframe is non-standard, it does
> seem to be a de facto standard. (...)


Hmm, first I wrote:

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

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

http://jorgechamorro.com/cljs/063/
--
Jorge.

 
Reply With Quote
 
Peter Michaux
Guest
Posts: n/a
 
      06-16-2009
On Jun 16, 12:36*pm, Jorge <(E-Mail Removed)> wrote:
> On Jun 16, 9:17*pm, Peter Michaux <(E-Mail Removed)> wrote:
>
> > (...)
> > Even though the "load" event of an iframe is non-standard, it does
> > seem to be a de facto standard. (...)

> 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

 
Reply With Quote
 
Andrew Poulos
Guest
Posts: n/a
 
      06-16-2009
Jorge wrote:
> On Jun 16, 9:17 pm, Peter Michaux <(E-Mail Removed)> wrote:
>> (...)
>> Even though the "load" event of an iframe is non-standard, it does
>> seem to be a de facto standard. (...)

>
> 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
 
Reply With Quote
 
Gabriel Gilini
Guest
Posts: n/a
 
      06-17-2009
On Jun 16, 6:00*pm, Peter Michaux <(E-Mail Removed)> wrote:
> On Jun 16, 12:36*pm, Jorge <(E-Mail Removed)> wrote:
>
> > On Jun 16, 9:17*pm, Peter Michaux <(E-Mail Removed)> wrote:

>
> > > (...)
> > > Even though the "load" event of an iframe is non-standard, it does
> > > seem to be a de facto standard. (...)

> > 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.


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.

--
Gabriel Gilini
 
Reply With Quote
 
Michael J. Ryan
Guest
Posts: n/a
 
      06-18-2009
On 6/16/2009 10:08 AM, Peter Michaux wrote:
> 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);

--
Michael J. Ryan - http://tracker1.info/

.... B5: I can only conclude that I'm paying off karma at a vastly accelerated
rate.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to reliably redirect from login page? =?Utf-8?B?QW5kcmV3?= ASP .Net 1 01-10-2006 04:45 PM
Custom Java sleeper thread not triggering reliably joshuamcgee@gmail.com Java 4 11-21-2005 04:17 AM
How can I detect when a file has completely loaded into an iframe? Mark Javascript 4 08-05-2004 01:17 PM
Get form values from iframe (1) to iframe (2) inside a layer in iframe (1) Daedalous Javascript 3 01-16-2004 11:08 AM
How to Detect Flash Reliably Maflu Javascript 0 10-29-2003 06:14 PM



Advertisments